我无法更新Tkinter中的行。将行设置为Tkinter中的变量
如果我将行设置为常规变量,它不会更新。这在第一个脚本中显示。 如果我将行设置为IntVar类型,就像使用文本一样,它会拒绝数据类型。这在第二个脚本中显示。
2注意事项: 如果您在脚本1中观看计数器,它的上升情况很好,但没有被应用。 如果使用self.activeRow.get()代替self.activeRow它将有效地把它变成具有相同的结果在脚本1.
所示脚本1
from tkinter import *
class Example(Frame):
def move(self):
self.activeRow += 1
print(self.activeRow)
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.columnconfigure(0, pad=0)
self.columnconfigure(1, pad=0)
self.columnconfigure(2, pad=0)
self.rowconfigure(0, pad=0)
self.rowconfigure(1, pad=0)
self.rowconfigure(2, pad=0)
Label(self, text= 'row 0').grid(row=0, column=0)
Label(self, text= 'row 1').grid(row=1, column=0)
Label(self, text= 'row 2').grid(row=2, column=0)
#regular variable
self.activeRow = 0
b = Button(self, text="normal variable {0}".format(self.activeRow), command=self.move)
b.grid(row=self.activeRow, column=1)
self.pack()
def main():
root = Tk()
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
脚本2
普通变量from tkinter import *
class Example(Frame):
def move(self):
self.activeRow.set(self.activeRow.get() + 1)
print(self.activeRow.get())
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.columnconfigure(0, pad=0)
self.columnconfigure(1, pad=0)
self.columnconfigure(2, pad=0)
self.rowconfigure(0, pad=0)
self.rowconfigure(1, pad=0)
self.rowconfigure(2, pad=0)
Label(self, text= 'row 0').grid(row=0, column=0)
Label(self, text= 'row 1').grid(row=1, column=0)
Label(self, text= 'row 2').grid(row=2, column=0)
#Tkinter IntVar
self.activeRow = IntVar()
self.activeRow.set(0)
b = Button(self, text="IntVar", command=self.move)
b.grid(row=self.activeRow, column=1)
self.pack()
您能否提供更完整的代码示例? 'row = self.activeRow'应该可以工作,前提是'self'具有为'activeRow'设置的值。 – cfedermann 2012-04-14 08:52:26
好的,我只是用更多的细节重写了它。 如果我的格式化/样式已关闭,请让我知道,因为我在6周前才开始学习如何编程。 – Talisin 2012-04-14 10:30:13
哦,只是为了节省您重新阅读它的问题是: 如果self.activeRow是一个正常的变量,它不会更新,如果它是一个IntVar,那么它不会被接受,因为它不是一个int。那么我怎么才能让它改变行呢? – Talisin 2012-04-14 10:39:12