如果存储在列表中,为什么PyTk Tkinter控制值变量无法更新?另一种选择是什么?

2024-03-28 23:33:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如果将对列表中对象的引用用作小部件的变量属性,则控制变量将无法工作。下面的代码演示了设置为TkInt对象列表的应用程序对象的示例。一个测试示例只是设置为一个成员变量,该变量是一个TkInt对象,可以按预期工作。 为什么会失败?管理小部件和控制数量可变的变量的正确方法是什么?你知道吗

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
        self.line_whole = [tk.IntVar() for _ in range(6)]

    def createWidgets(self):
        tk
        self.line_whole=list()
        #self.line_whole = [tk.IntVar() for _ in range(6)]
        self.line_whole_cb = list()
        for x in range(6):
            self.line_whole.append(tk.IntVar())
            self.line_whole_cb.append(tk.Checkbutton(self,variable=self.line_whole[x]))
            self.line_whole_cb[x].pack(side="left")
        self.test_val = tk.IntVar()
        self.test_cb = tk.Checkbutton(self,variable=self.test_val)
        self.test_cb.pack(side="bottom")
        self.output_record_btn = tk.Button(self)
        self.output_record_btn["text"] = "Output Check Values"
        self.output_record_btn["command"] = self.say_hi
        self.output_record_btn.pack(side="top")

        self.QUIT = tk.Button(self, text="QUIT", fg="red",
                                            command=root.destroy)
        self.QUIT.pack(side="bottom")

    def say_hi(self):
        print("checks:",",".join([str(tk_int.get()) for tk_int in self.line_whole]))
        print ("test_check",self.test_val.get())

root = tk.Tk()
app = Application(master=root)
app.mainloop()

Tags: 对象intestselfforoutputlinerecord
1条回答
网友
1楼 · 发布于 2024-03-28 23:33:20

下面是一个经过修改的版本,改动很小:

在设置self.line_whole之前,您正在调用createWidgets。我想这就是你重新创造它的原因。你只需要改变你所说的顺序。次要的是,如果您不打算更改onvalueoffvalue属性,为什么不使用BooleanVar()?因为它只能取0和1,这是复选按钮off和on值的默认值。当然,任何一种方法都是可行的,选择仅仅是语义。你知道吗

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.line_whole = [tk.IntVar() for _ in range(6)]
        self.createWidgets()

    def createWidgets(self):
        #self.line_whole = [tk.IntVar() for _ in range(6)]
        self.line_whole_cb = list()
        for x in range(6):
            self.line_whole_cb.append(tk.Checkbutton(self, 
                variable=self.line_whole[x]))
            self.line_whole_cb[x].pack(side="left")
        self.test_val = tk.IntVar()
        self.test_cb = tk.Checkbutton(self,variable=self.test_val)
        self.test_cb.pack(side="bottom")
        self.output_record_btn = tk.Button(self)
        self.output_record_btn["text"] = "Output Check Values"
        self.output_record_btn["command"] = self.say_hi
        self.output_record_btn.pack(side="top")

        self.QUIT = tk.Button(self, text="QUIT", fg="red",
                                            command=root.destroy)
        self.QUIT.pack(side="bottom")

    def say_hi(self):
        print("checks:",",".join([str(tk_int.get()) for tk_int in self.line_whole]))
        print ("test_check",self.test_val.get())

root = tk.Tk()
app = Application(master=root)
root.mainloop()

相关问题 更多 >