python 显示字典内容_在pythontkin的标签中显示字典内容
Label(..., text= x)將只顯示一次-在開始處。在
您必須使用StringVar來完成您期望的操作x_var = StringVar()
Label(..., texvariable=x_var)
現在您可以在x_var中設置值,以自動在Label中設置新文本
^{pr2}$
順便說一句:不是
^{3}$
也許你該這么做x = list(d.keys())
或者甚至x = list(d)
編輯:按“顯示”按鈕時顯示所有鍵(如代碼中所示)import sys
from Tkinter import *
import ttk
import time
from datetime import datetime
# - functions
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
stock = stock_Entry.get()
Quantity = Quantity_Entry.get()
if stock not in d:
d[stock] = Quantity
else:
d[stock] += Quantity
# - main -
x = list()
d = dict()
now = datetime.now()
root = Tk()
root.title("Homework 5 216020088")
x_var = StringVar()
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()
總結
以上是生活随笔為你收集整理的python 显示字典内容_在pythontkin的标签中显示字典内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Service Mesh(服务网格)——
- 下一篇: iPhone 12易掉漆、边框太锋利还割