首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter更新字典菜单字典上的标签

Tkinter更新字典菜单字典上的标签
EN

Stack Overflow用户
提问于 2017-05-26 15:51:49
回答 1查看 791关注 0票数 0

在这里,我们使用一个GUI选择一个手机和自动更新一个handset_cost。问题是如何在小部件字典中更新tkinter textvariable。

我想扩展这个更简单的解决方案:Updating Label text after OptionMenu selection changes

您将从下面的代码中看到,当您选择手机时,只有最后一行被更新(不正确)。我已经尝试了我能想到的一切,但是我太缺乏经验了,无法了解如何获得函数'displayPrice‘来引用每一行的值。你能帮忙吗谢谢。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-23 15:07:18

工作解决方案与原来的代码有很大的不同。

我已经发布了一个解决方案,以防对某人有帮助。

手机和handset_cost变量最初是分开的,因为我们不希望字符串和浮动组件放在一个变量中。但是,解决方案是将它们简单地合并为一个单独的字段:

代码语言:javascript
复制
HLabel9= tk.Label(table, text = "Proposed_Handset_&_cost")


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

然后提取值,例如:

代码语言:javascript
复制
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('}'))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44205614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档