在 Tkinter Python 中显示字典
Posted
技术标签:
【中文标题】在 Tkinter Python 中显示字典【英文标题】:Display Dictionaries in Tkinter Python 【发布时间】:2017-01-25 12:34:01 【问题描述】:我编写了一个简单的 Tkinter 程序,我试图同时显示我的股票名称和股票数量,但我发现只有我的股票名称显示。我的代码如下:
import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime
now= datetime.now()
x = []
d = dict()
def quit():
print("Have a great day! Goodbye :)")
sys.exit(0)
def display():
x_var.set(list(d))
def add(*args):
global stock
global d
global Quantity
stock = stock_Entry.get()
Quantity = int(Quantity_Entry.get())
if stock not in d:
d[stock] = Quantity
else:
d[stock] += Quantity
root = Tk()
root.title("Homework 5 216020088")
x_var = StringVar()
x_var.set(x)
mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (now.day,now.month,now.year)+" at exactly %s:%s:%s" % (now.hour,now.minute,now.second), foreground="yellow", background="Black").grid(column=0, row = 0)
stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock")
stock_Entry.grid(column=0, row = 1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row = 1, sticky=(N, W, E, S))
Quantity_Entry= ttk.Entry(mainframe, width = 60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row = 2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row = 2, sticky=(N, W, E, S))
ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, textvariable= x_var).grid(column=0, row= 4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
现在,这只是显示我的库存而不是数量,你能帮我或建议我应该怎么做吗?
【问题讨论】:
【参考方案1】:目前,在您的 display()
函数中,您只更改指向股票的 x_var
。
您是否尝试过添加另一个 tk.Label
并将数量变量绑定到它?
【讨论】:
是的,但我还是什么都没有,你能告诉我怎么做吗【参考方案2】:你可以试试这个:
import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime
now = datetime.now()
x = []
d =
def quit():
print("Have a great day! Goodbye :)")
sys.exit(0)
def display():
x = []
for key, value in d.items():
x.append([key, value])
print(x)
x_var.set(x)
def add(*args):
global stock
global d
global Quantity
stock = stock_Entry.get()
Quantity = int(Quantity_Entry.get())
if stock not in d:
d[stock] = Quantity
else:
d[stock] += Quantity
root = Tk()
root.title("Homework 5 216020088")
x_var = StringVar()
x_var.set(x)
mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (
now.day, now.month, now.year) + " at exactly %s:%s:%s" % (now.hour, now.minute, now.second), foreground="yellow",
background="Black").grid(column=0, row=0)
stock_Entry = ttk.Entry(mainframe, width=60, textvariable="stock")
stock_Entry.grid(column=0, row=1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row=1, sticky=(N, W, E, S))
Quantity_Entry = ttk.Entry(mainframe, width=60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row=2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row=2, sticky=(N, W, E, S))
ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, textvariable=x_var).grid(column=0, row=4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
【讨论】:
以上是关于在 Tkinter Python 中显示字典的主要内容,如果未能解决你的问题,请参考以下文章
如何在python中每隔几秒在tkinter窗口中更改一行文本[重复]
无法在终端执行使用 tkinter 和 py2app 创建的 Mac 应用程序