Python Tkinter更新选项菜单上的标签字典
在这里,我们使用GUI选择手机并自动更新手机。 问题是如何在小部件字典中更新tkinter textvariable 我想扩展这个更简单的解决方案: 通过运行下面的代码,您将看到,当您选择手机时,只有最后一行(错误地)被更新。我已经尝试了所有我能想到的方法,但是我太没有经验了,不知道如何让函数“displayPrice”为每一行引用一个值。请帮忙,谢谢Python Tkinter更新选项菜单上的标签字典,python,user-interface,dictionary,tkinter,Python,User Interface,Dictionary,Tkinter,在这里,我们使用GUI选择手机并自动更新手机。 问题是如何在小部件字典中更新tkinter textvariable 我想扩展这个更简单的解决方案: 通过运行下面的代码,您将看到,当您选择手机时,只有最后一行(错误地)被更新。我已经尝试了所有我能想到的方法,但是我太没有经验了,不知道如何让函数“displayPrice”为每一行引用一个值。请帮忙,谢谢 import tkinter as tk import datetime class Example(tk.Frame): def
import tkinter as tk
import datetime
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, background="black")
root.title("Mobile Order Quote")
table = tk.Frame(self, background="black")
table.pack(side="top", fill="both", expand=True)
data = [(1, ),(2, ),(3, ),(5, )]
handset_dict1 = {'apple_iphone': 500.0, 'two_cans_plus_string': 50.0, 'samsung_galaxy': 800.0, 'none': 0.0}
table = tk.Frame(self, background="black")
table.pack(side="top", fill="both", expand=True)
self.widgets = {}
# MAKE 'big_tuple': solving the list of tuples problem - make a tuple of tuples and format too
x = list(data)
list_of_lists = [list(elem) for elem in x]
big_list = []
for i in list_of_lists:
data1=(str(i[0]))
big_list.append(data1)
big_tuple = tuple(big_list)
#global big_tuple
row = 0
for rent_id in (big_tuple):
HLabel0 = tk.Label(table, text = "ID", fg = "white", background="black")
HLabel9 = tk.Label(table, text = "Proposed_Handset", fg = "white", background="black")
HLabel10 = tk.Label(table, text = "Handset_Cost", fg = "white", background="black")
HLabel0.grid(row = 0, column = 0, padx=1, pady=1)
HLabel9.grid(row = 0, column = 9, padx=1, pady=1)
HLabel10.grid(row = 0, column = 10, padx=1, pady=1)
row += 1
handset = tk.StringVar(root) # creates tkvar for the handsets
handset.set('none')
handsetCost = tk.DoubleVar(root)
handsetCost.set(0)
def displayPrice(value):
handsetCost.set(handset_dict1[value])
self.widgets[rent_id] = {
"rent_id": tk.Label(table, text=rent_id),
"handset": tk.OptionMenu(table, handset, *handset_dict1.keys(), command=displayPrice, ),
"handset_cost": tk.Label(table, textvariable =handsetCost), }
self.widgets[rent_id]["rent_id"].grid(row=row, column=0, sticky="nsew", padx=1, pady=1)
self.widgets[rent_id]["handset"].grid(row=row, column=9, sticky="nsew", padx=1, pady=1)
self.widgets[rent_id]["handset_cost"].grid(row=row, column=10, sticky="nsew", padx=1, pady=1)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
工作解决方案与原始代码有很大的不同 我已经发布了一个解决方案,以防对某人有所帮助 handset和handset_成本变量最初是分开的,因为我们不希望字符串和浮点组件一起包含在一个变量中。 但是,解决方案是将它们简单地合并为一个字段:
HLabel9= tk.Label(table, text = "Proposed_Handset_&_cost")
"handset": ttk.Combobox(table, textvariable=handset,
values[*handset_dict1], ),
然后,我们提取了这些值,例如:
hs_buy_list = []
for rent_id in (sorted(self.widgets.keys())):
hs_buy_price = self.widgets[rent_id]['handset'] # handset and buy
new_hs_buy = hs_buy_price.get()
hs_buy_list.append(new_hs_buy)
buy_hs_int = [] # splits out the buy price of handsets
for i in hs_buy_list:
buy_hs_int.append(i.split(':')[1].rstrip('}'))
这是一段非常长的代码。你有没有办法把它减少到影响结果的关键部分?@asongtoruin-谢谢你,我同意。我已经缩短了它,删除了所有的按钮和功能。我会进一步检查。这个代码需要减少。例如,您真的需要13个标签小部件吗?为什么你不能用一两个来回答这个问题?具体的字体或页面标题真的很重要吗?只需减少标签的数量,就可以删除40多行。@BryanOakley-谢谢。另一个新手经验教训。希望现在,这是一个可以接受的代码,可以请求别人的帮助。@asongtoruin-对不起,我以前用过我臃肿的代码,浪费了你的时间。我可以礼貌地重新引起你对这个问题的兴趣吗?