簡體   English   中英

Tkinter更新字典選項菜單詞典上的標簽

[英]Tkinter update label on optionmenu dictionary of dictionaries

在這里,我們使用GUI選擇一個手機並自動更新聽筒成本。 問題是如何更新窗口小部件字典中的tkinter textvariable。

我想擴展這個更簡單的解決方案: 在OptionMenu選擇更改后更新標簽文本

通過運行以下代碼,您會看到,選擇聽筒時,僅最后一行(錯誤地)被更新。 我已經嘗試了所有可以想到的方法,但是我實在經驗不足,無法看到如何獲取函數“ displayPrice”來引用每一行的值。 請幫忙,謝謝。

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

工作解決方案與原始代碼有很大的不同。

我已經發布了一個解決方案,以防它對某人有所幫助。

手機變量和手機變量成本最初是分開的,因為我們不希望將字符串和浮點分量放到一個變量中。 但是,解決方案是將它們簡單地組合為一個字段:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM