简体   繁体   English

Tkinter更新字典选项菜单词典上的标签

[英]Tkinter update label on optionmenu dictionary of dictionaries

Here we use a GUI to select a handset and auto update a handset_cost. 在这里,我们使用GUI选择一个手机并自动更新听筒成本。 The problem is how to update a tkinter textvariable in a dictionary of widgets. 问题是如何更新窗口小部件字典中的tkinter textvariable。

I'd like to extend this simpler solution: Updating Label text after OptionMenu selection changes 我想扩展这个更简单的解决方案: 在OptionMenu选择更改后更新标签文本

You'll see from running the code below, only the final row is (incorrectly) updated when you select a handset. 通过运行以下代码,您会看到,选择听筒时,仅最后一行(错误地)被更新。 I've tried everything I can think of but I'm too inexperienced to see how to get the function 'displayPrice' to reference a value for each row. 我已经尝试了所有可以想到的方法,但是我实在经验不足,无法看到如何获取函数“ displayPrice”来引用每一行的值。 Please can you help, thanks. 请帮忙,谢谢。

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()

The working solution changed considerably from the original code. 工作解决方案与原始代码有很大的不同。

I've posted a solution in case it helps someone. 我已经发布了一个解决方案,以防它对某人有所帮助。

The handset and handset_cost variables were initially separated because we didn't want the string and float components together in one variable. 手机变量和手机变量成本最初是分开的,因为我们不希望将字符串和浮点分量放到一个变量中。 However, the solution was to simply combine them as a single field: 但是,解决方案是将它们简单地组合为一个字段:

HLabel9= tk.Label(table, text = "Proposed_Handset_&_cost")


"handset": ttk.Combobox(table, textvariable=handset, 
               values[*handset_dict1], ),

We then extracted the values, for example: 然后,我们提取值,例如:

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('}'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM