gpt4 book ai didi

python - Tkinter 为每个单选按钮返回相同的值?

转载 作者:行者123 更新时间:2023-12-01 06:54:32 24 4
gpt4 key购买 nike

我正在使用 Tkinter 弹出一个自定义对话框。我从另一个 Tkinter 窗口打开它。

root = Tk()
class ListDialog:
def __init__(self, names, prompt):
self.names = names
self.sub_root = Tk()
self.sub_root.title("Intovex")
self.sub_root.iconbitmap("Icon.ico")
self.myfont = Font(root=self.sub_root, family="Arial", size=8)
self.sub_root.maxsize(320, 240)
self.sub_root.wm_attributes("-topmost", True)
self.sub_root.wm_attributes("-toolwindow", True)
self.var = IntVar()
label = Label(self.sub_root, text=prompt)
label.pack(fill=X)
c=1
print(names)
for i in names:
print(i)
r = Radiobutton(self.sub_root, text=i, variable=self.var, value=c, command=self.end)
r.pack(anchor=W)
c+=1
self.var.set(1)
button = Button(self.sub_root, command=self.endit, text="OK", bg = "#448DE0", fg="White", bd=0, width=12, pady=4, padx=4, height=1,font=self.myfont, highlightcolor="#A3C7F0")
button.pack(side=BOTTOM)
self.choice = names[0]

def end(self):
ch = self.var.get()
print(str(ch))
self.choice = self.names[ch - 1]

def endit(self):
self.sub_root.destroy()

def ask(self):
self.sub_root.mainloop()
var2 = StringVar()
def get_choice():
list = ListDialog(["Test1", "Test2"], "Testing")
list.ask()
var2.set(str(list.choice))

label = Label(root, text="", textvariable=var2)
button = Button(root, text="Test", command=get_choice)
label.pack()
button.pack()
root.mainloop()

但是,当它通过直接实例化类并调用ask()方法单独运行时,它是有效的。您可能已经看到我在代码中到处都有 print 语句(用于调试),并且我发现它在哪里不起作用

  • 用于打印 names 列表参数的 print 语句正确打印整个列表。
  • 在 for 循环内,它还会正确打印 names 列表中的元素
  • 当我单击单选按钮时,它会调用 end() 方法。它总是打印默认值。
root = Tk()
class ListDialog:
def __init__(self, names, prompt):
self.names = names
self.sub_root = Tk()
self.sub_root.title("Intovex")
self.sub_root.iconbitmap("Icon.ico")
self.myfont = Font(root=self.sub_root, family="Arial", size=8)
self.sub_root.maxsize(320, 240)
self.sub_root.wm_attributes("-topmost", True)
self.sub_root.wm_attributes("-toolwindow", True)
self.var = IntVar()
label = Label(self.sub_root, text=prompt)
label.pack(fill=X)
c=1
print(names)
for i in names:
print(i)
r = Radiobutton(self.sub_root, text=i, variable=self.var, value=c, command=self.end)
r.pack(anchor=W)
c+=1
self.var.set(1)
button = Button(self.sub_root, command=self.endit, text="OK", bg = "#448DE0", fg="White", bd=0, width=12, pady=4, padx=4, height=1,font=self.myfont, highlightcolor="#A3C7F0")
button.pack(side=BOTTOM)
self.choice = names[0]

def end(self):
ch = self.var.get()
print(str(ch))
self.choice = self.names[ch - 1]

def endit(self):
self.sub_root.destroy()

def ask(self):
self.sub_root.mainloop()
list = ListDialog(["Test1", "Test2"], "Testing")
list.ask()
print(list.choice)

但如果我将它作为顶级小部件打开,它就会起作用。但是主窗口不会等到弹出窗口返回值(选择)。

最佳答案

第一个代码片段中的代码存在问题,因为您在 tkinter 应用程序中多次调用 Tk() — 它会混淆接口(interface)代码并可能导致各种问题,例如你就会发现。

如果将 ListDialog 类的 __init__() 方法中的调用替换为 1 到 tk.Toplevel()相反,您的代码将开始工作。

我还通过更改它来简化创建 Radiobuttonfor 循环,以便使用内置 enumerate()功能自动记录姓名计数。与此同时,我将 IntVar 的初始值设置为,这不是选择单选按钮将分配给它的值之一。因此,当 ListDialog 首次显示时,它们都不会被选择。它还确保每当用户按下其中一个回调函数时都会调用 end() 回调函数,因此您的应用程序将始终在发生这种情况时收到通知。

请注意,虽然我没有更改它,但您不应该命名变量list,因为这会通过该名称隐藏内置类的名称。一般来说,您应该避免命名任何与现有标准 Python 名称冲突的内容。

from tkinter import *
from tkinter.font import Font

root = Tk()

class ListDialog:
def __init__(self, names, prompt):
self.names = names
# self.sub_root = Tk() # Wrong - don't call Tk() more than once.
root.withdraw() # Hide root window.
self.sub_root = Toplevel() # Create another top-level window.
self.sub_root.title("Intovex")
# self.sub_root.iconbitmap("Icon.ico") # I don't have this file...
self.myfont = Font(root=self.sub_root, family="Arial", size=8)
self.sub_root.maxsize(320, 240)
self.sub_root.wm_attributes("-topmost", True)
self.sub_root.wm_attributes("-toolwindow", True)
self.var = IntVar(value=0) # Define and init value to one *not* produced by btns.
label = Label(self.sub_root, text=prompt)
label.pack(fill=X)
print(names)
for c, name in enumerate(names, start=1):
print(c)
r = Radiobutton(self.sub_root, text=c, variable=self.var, value=c,
command=self.end)
r.pack(anchor=W)

button = Button(self.sub_root, command=self.endit, text="OK", bg = "#448DE0",
fg="White", bd=0, width=12, pady=4, padx=4, height=1,
font=self.myfont, highlightcolor="#A3C7F0")
button.pack(side=BOTTOM)
self.choice = names[0]

def end(self):
ch = self.var.get()
print(str(ch))
self.choice = self.names[ch - 1]

def endit(self):
self.sub_root.destroy()
root.deiconify() # Reshow root window.

def ask(self):
self.sub_root.mainloop()

var2 = StringVar()

def get_choice():
list = ListDialog(["Test1", "Test2"], "Testing")
list.ask()
var2.set(str(list.choice))

label = Label(root, text="", textvariable=var2)
button = Button(root, text="Test", command=get_choice)
label.pack()
button.pack()
root.mainloop()

关于python - Tkinter 为每个单选按钮返回相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com