Python - 在一个标签中打印组合框值

时间:2015-05-13 17:07:43

标签: python-3.x tkinter

如何创建包含combo1和combo2中所选值的值的标签。而不是2个单独的标签(A_ANA,A_AGR或B_AGR等)?

我希望能够在该标签上生成A_ANA或B_ana等名称

import tkinter as tk
from tkinter import ttk


class LabeledCombobox(tk.Frame):

    def __init__(self, master, dictionary, *args, **kw):
        tk.Frame.__init__(self, master, *args, **kw)
        self.dictionary = dictionary

        self.combo = ttk.Combobox(self, values=sorted(list(dictionary.keys())),
                                  state='readonly')
        self.combo.current(0)
        self.combo.pack(fill="both")
        self.combo.bind('<<ComboboxSelected>>', self.on_selection)

        self.label = tk.Label(self, text=self.value())
        self.label.pack(fill="both", expand=True)


    def value(self):
        return self.dictionary[self.combo.get()]

    def on_selection(self, event=None):  # Just to test
        self.label.config(text=self.value())


lookup = {'Arkitekt': 'A', 'Geoteknik': 'B',
          'Ingeniør Anlæg': 'C', 'Procesanlæg': 'D'}

documentcode = {'Aftaler': 'AGR', 'Analyse': 'ANA',
                'Myndigheder': 'AUT', 'Sagsbasis': 'BAS'}


if __name__ == "__main__":
    root = tk.Tk()
    root.title("Labeled comboboxes")

    combo1 = LabeledCombobox(root, lookup, bd=1, relief="groove")
    combo1.pack(side="left", padx=(2, 2), pady=5)
    combo2 = LabeledCombobox(root, documentcode, bd=1, relief="groove")
    combo2.pack(side="right", padx=(2, 2), pady=5)

    root.mainloop()

1 个答案:

答案 0 :(得分:0)

您需要从两个组合框中获取值,将它们组合成一个字符串,然后使用configure方法修改标签(如果您正在使用,则更新关联的StringVar) :

newlabel = "%s_%s" % (combo1.value(), combo2.value())
label.configure(text=newlabel)
相关问题