我是一名业余Python程序员,有2个月的经验。我正在尝试通过 tkinter 编写一个 GUI 待办事项列表。按钮的实际位置并不重要。之后我可以玩那些。我需要一些帮助来将附加项目显示到列表中。在程序中,它在数字上更新得很好,但不会打印到列表上。我在控制台上仔细检查了它,它显示“tkinter.StringVar object at 0x102fa4048”,但没有更新实际列表。我需要帮助的是如何更新标签列上的 Main_Q 列表?非常感谢一些指导和编码帮助。谢谢。
Main_Q =["read","clean dishes", "wash car"]
from tkinter import*
root=Tk(className="total tasks in the Q")
#formula
def update():
global Main_Q
a=len(Main_Q)
num.set(a)
def add2list():
Main_Q.append(name)
a=len(Main_Q)
num.set(a)
print (Main_Q)
#output
num=StringVar()
y=Label(root, textvariable=num).grid(row=0, column=1)
#input
name=StringVar()
b=Entry(root, textvariable=name).grid(row=7,column=0)
#buttons
z=Button(root, text="update", command=update).grid(row=7, column=2)
add2list=Button(root,text="add", command=add2list).grid(row=7,
column=1)
r = 0
for c in Main_Q:
Label(text=c, relief=RIDGE,width=15).grid(row=r,column=0)
r = r + 1
root.mainloop()
您的问题是,每次您输入新的“任务”后,构建标签的 for 循环都不会被调用。要轻松解决此问题,您可以将此循环移至更新函数中。
如果您想防止每次都循环遍历小部件,您可以创建一个包含已创建的所有小部件的新列表:
createdWidgets = []
widgetsQueue = []
在更新函数中,您必须迭代
widgetsQueue
(例如widgetsQueue.pop()),创建小部件并将小部件附加到createdWidgetes
列表中。
def update():
global Main_Q
r = 0
for c in Main_Q:
Label(text=c, relief=RIDGE,width=15).grid(row=r,column=0)
r += 1 # shorthand for r = r + 1
一些补充说明:
对于条目来说,将定义和放置分开更容易:
b = Entry(root)
b.grid(row=7,column=0)
因为 Entry() 返回其实例,您可以使用它来获取文本:
b.get()
如果你去购物,你会把所有东西都扔进一个袋子里吗?
from tkinter import *
确实如此(在这种情况下,globals()变量就是包)。如果你想了解更多关于导入Python模块的信息。为了防止这种情况并减少输入的字母数量:
import tkinter as t # or tk
root = t.Tk()
*但是可以肯定的是,如果您只想要一个小程序,那也可以。
要解决您的问题,您需要设计这个简单的解决方案:
Tkinter.Entry
方法检索
get()
小部件的文本。Main_Q
方法将 1中获得的文本添加到
append()
。Main_Q
方法绑定单击
command
和 GUI 时更新的按钮。我更喜欢在包含构造函数的类中组织代码,其中
Main_Q
已初始化,以便我们调用 initialize_user_interface()
来使用其三个元素初始化 GUI:
def __init__(self, parent):
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.Main_Q = ["read", "clean dishes", "wash car"]
self.r = 0 # position of the row of each label
self.initialize_user_interface()
方法
initialize_user_interface()
正如其名称所示。我们主要绑定函数 update_gui()
,该函数插入一个新标签,并将文本设置为用户使用 Tkinter.Entry
在
command = self.update_gui
小部件中键入的内容
ef initialize_user_interface(self):
self.parent.title("Update GUI")
self.parent.grid_rowconfigure(0, weight = 1)
self.parent.grid_columnconfigure(0, weight = 1)
for e in self.Main_Q:
Tkinter.Label(self.parent, anchor = Tkinter.W, text = e).grid(row = self.r, sticky = Tkinter.W)
self.r+=1
self.entry_text = Tkinter.Entry(self.parent)
self.entry_text.grid(row = 0, column = 1)
self.button_update = Tkinter.Button(self.parent, text = "Update", command = self.update_gui).grid(row = 1, column = 1, sticky = Tkinter.E)
最后,没有什么比
update_gui()
函数更简单了:
def update_gui(self):
self.r+=1 # increment the row reserved to the new label
self.Main_Q.append(self.entry_text.get())
Tkinter.Label(self.parent, anchor = Tkinter.W, text = self.entry_text.get()).grid(row = self.r, sticky = Tkinter.W)
这是完整的程序:
'''
Created on Mar 11, 2016
@author: Bill BEGUERADJ
'''
import Tkinter
class Begueradj(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.main_queue = ["read", "clean dishes", "wash car"]
self.r = 0
self.initialize_user_interface()
def initialize_user_interface(self):
self.parent.title("Update GUI")
self.parent.grid_rowconfigure(0, weight = 1)
self.parent.grid_columnconfigure(0, weight = 1)
for e in self.main_queue:
Tkinter.Label(self.parent, anchor = Tkinter.W, text = e).grid(row = self.r, sticky = Tkinter.W)
self.r+=1
self.entry_text = Tkinter.Entry(self.parent)
self.entry_text.grid(row = 0, column = 1)
self.button_update = Tkinter.Button(self.parent, text = "Update", command = self.update_gui).grid(row = 1, column = 1, sticky = Tkinter.E)
def update_gui(self):
self.r+=1
self.main_queue.append(self.entry_text.get())
Tkinter.Label(self.parent, anchor = Tkinter.W, text = self.entry_text.get()).grid(row = self.r, sticky = Tkinter.W)
def main():
root = Tkinter.Tk()
b = Begueradj(root)
root.mainloop()
if __name__ == "__main__":
main()
这是运行程序的截图:
我之前的程序是用Python 2.7编写的,所以如果你想测试它,请将
Tkinter
更改为tkinter
。其他一切都保持不变。