【问题标题】:How to make the text of a Label change automatically?如何使标签的文本自动更改?
【发布时间】:2021-09-01 21:30:21
【问题描述】:

我在选择新的Combobox 选项时自动更新ttk.Label(frame, text=value).grid(column=1, row=2) 时遇到问题。我知道这与我的处理函数有关,但对于我的一生,我一直无法弄清楚。

import tkinter as tk
from tkinter import ttk
from tkinter import *
from time import *

vegetable_needs = {
    'Tomato': {'ph': '6', 'moisture': '60', 'humidity': '70'},
    'Bell pepper': {'ph': '9', 'moisture': '61', 'humidity': '71'},
    'Cucumber': {'ph': '2', 'moisture': '62', 'humidity': '72'},
    'Broccoli': {'ph': '4', 'moisture': '63', 'humidity': '73'},
    'Green Bean': {'ph': '1', 'moisture': '64', 'humidity': '74'},
    'Zucchini': {'ph': '5', 'moisture': '65', 'humidity': '75'},
    'Sweet potatoe': {'ph': '0', 'moisture': '66', 'humidity': '76'}
}

#Create Frame
def create_input_frame(container):
    #declare string variables
    vegetable = tk.StringVar()

    frame = tk.Frame(container, borderwidth=1, relief=RIDGE, padx=15, pady=10)
    
    options = ttk.Combobox(frame, width = 20, textvariable = vegetable)
    #list of veggies
    options['values'] = (
    "Tomato",
    "Bell pepper",
    "Cucumber",
    "Broccoli",
    "Green Bean",
    "Zucchini",
    "Sweet potatoe"
    )
    options['state'] = 'readonly'

    ttk.Label(frame, text = "Select a veggie: ", font = ("Times New Roman", 10)).grid(column=0, row=0, padx=5, pady=0)
    options.grid(column = 1, row = 0)
    options.current()
    

    def handler(event):
        current = options.current()
        if current != -1:
            ph_level = vegetable_needs[vegetable.get()]['ph']
            value = ph_level
            print(value)
    
    
    options.bind('<<ComboboxSelected>>', handler)
    
    ttk.Label(frame, text=value).grid(column=1, row=2)
    ttk.Label(frame, text='PH Level').grid(column=0, row=2)
  
    return frame

#Create the main window
def create_main_window():
    root = tk.Tk()
    root.title('Growing Veggies')
    root.attributes('-toolwindow', False)
    r1=0
    r2=0
    r3=0
    
    #Create zones 1-5 row 0
    if number_of_zones >= 1 and number_of_zones <= 5:
        for r1 in range(number_of_zones):
            input_frame = create_input_frame(root)
            input_frame.grid(column=r1, row=0)
            r1+=1
    #Create zones 6-10 rows 0-1
    elif number_of_zones > 5 and number_of_zones <=10:
        for r1 in range(5):
            input_frame = create_input_frame(root)
            input_frame.grid(column=r1, row=0)
            r1+=1
        for r2 in range(number_of_zones - 5):
            input_frame = create_input_frame(root)
            input_frame.grid(column=r2, row=1)
            r2+=1
    #Create zones 11-15 rows 0-2
    elif number_of_zones > 10 and number_of_zones <=15:
        for r1 in range(5):
            input_frame = create_input_frame(root)
            input_frame.grid(column=r1, row=0)
            r1+=1
        for r2 in range(5):
            input_frame = create_input_frame(root)
            input_frame.grid(column=r2, row=1)
            r2+=1
        for r2 in range(number_of_zones - 10):
            input_frame = create_input_frame(root)
            input_frame.grid(column=r2, row=2)
            r3+=1

    #Output an error
    else:
        root.title('ERROR')
        ttk.Label(root, text = "Please enter a valid number between 1 and 15").grid(column=1, row=1, padx=15, pady=5)
        ttk.Button(root, text = "Acknowledge", command=quit).grid(column=1, row=2, padx=5, pady=5)
    
    root.mainloop()

if __name__ == "__main__":

    value = "?"
    number_of_zones = int(input("How many zones should be made? "))
    create_main_window()
'''

【问题讨论】:

    标签: python string dictionary tkinter combobox


    【解决方案1】:

    您需要创建变量来引用您的标签。此外,您需要在更改 PH 时更新它们。以下是代码的外观,更改标记为### EDITED LINE### ADDED LINE

    import tkinter as tk
    from tkinter import ttk
    from tkinter.constants import *
    
    vegetable_needs = {
        'Tomato': {'ph': '6', 'moisture': '60', 'humidity': '70'},
        'Bell pepper': {'ph': '9', 'moisture': '61', 'humidity': '71'},
        'Cucumber': {'ph': '2', 'moisture': '62', 'humidity': '72'},
        'Broccoli': {'ph': '4', 'moisture': '63', 'humidity': '73'},
        'Green Bean': {'ph': '1', 'moisture': '64', 'humidity': '74'},
        'Zucchini': {'ph': '5', 'moisture': '65', 'humidity': '75'},
        'Sweet potatoe': {'ph': '0', 'moisture': '66', 'humidity': '76'}
    }
    
    #Create Frame
    def create_input_frame(container):
        #declare string variables
        vegetable = tk.StringVar()
    
        frame = tk.Frame(container, borderwidth=1, relief=RIDGE, padx=15, pady=10)
        
        options = ttk.Combobox(frame, width = 20, textvariable = vegetable)
        #list of veggies
        options['values'] = (
        "Tomato",
        "Bell pepper",
        "Cucumber",
        "Broccoli",
        "Green Bean",
        "Zucchini",
        "Sweet potatoe"
        )
        options['state'] = 'readonly'
    
        veggie_label = ttk.Label(frame, text = "Select a veggie: ", font = ("Times New Roman", 10)) ### EDITED LINE
        veggie_label.grid(column=0, row=0, padx=5, pady=0) ### ADDED LINE
        options.grid(column = 1, row = 0)
        options.current()
        
    
        def handler(event):
            current = options.current()
            if current != -1:
                ph_level = vegetable_needs[vegetable.get()]['ph']
                value = ph_level
                value_label.config(text=value) ### ADDED LINE
                print(value)
        
        
        options.bind('<<ComboboxSelected>>', handler)
        
        value_label = ttk.Label(frame, text=value) ### EDITED LINE
        value_label.grid(column=1, row=2) ### ADDED LINE
        ph_label = ttk.Label(frame, text='PH Level') ### EDITED LINE
        ph_label.grid(column=0, row=2) ### ADDED LINE
        
        return frame
    
    #Create the main window
    def create_main_window():
        root = tk.Tk()
        root.title('Growing Veggies')
        # root.attributes('-toolwindow', False)
        r1=0
        r2=0
        r3=0
        
        #Create zones 1-5 row 0
        if number_of_zones >= 1 and number_of_zones <= 5:
            for r1 in range(number_of_zones):
                input_frame = create_input_frame(root)
                input_frame.grid(column=r1, row=0)
                r1+=1
        #Create zones 6-10 rows 0-1
        elif number_of_zones > 5 and number_of_zones <=10:
            for r1 in range(5):
                input_frame = create_input_frame(root)
                input_frame.grid(column=r1, row=0)
                r1+=1
            for r2 in range(number_of_zones - 5):
                input_frame = create_input_frame(root)
                input_frame.grid(column=r2, row=1)
                r2+=1
        #Create zones 11-15 rows 0-2
        elif number_of_zones > 10 and number_of_zones <=15:
            for r1 in range(5):
                input_frame = create_input_frame(root)
                input_frame.grid(column=r1, row=0)
                r1+=1
            for r2 in range(5):
                input_frame = create_input_frame(root)
                input_frame.grid(column=r2, row=1)
                r2+=1
            for r2 in range(number_of_zones - 10):
                input_frame = create_input_frame(root)
                input_frame.grid(column=r2, row=2)
                r3+=1
    
        #Output an error
        else:
            root.title('ERROR')
            error_label = ttk.Label(root, text = "Please enter a valid number between 1 and 15")
            error_label.grid(column=1, row=1, padx=15, pady=5)
            ttk.Button(root, text = "Acknowledge", command=quit).grid(column=1, row=2, padx=5, pady=5)
        
        root.mainloop()
    
    if __name__ == "__main__":
    
        value = "?"
        number_of_zones = int(input("How many zones should be made? "))
        create_main_window()
    

    请注意,我将标签定义为变量:value_labelph_labelerror_label。这使得标签可以在整个程序中被引用。还要注意在handler 函数中,我如何调用value_label.config(text=value)。这会将 value_label 的文本设置为 PH 级别。

    【讨论】:

    • 非常感谢!那讲得通。我知道我错过了类似的东西,但无论我尝试什么都无法弄清楚。我很感激
    • 请记住以后再这样做。可以省去很多麻烦!编码愉快!
    • @SamMatzko 您的回答是正确的,但您应该避免从模块中导入所有内容。此外,您两次导入了tkinter。这只是一个建议。谢谢!
    • @IshaanJog 谢谢!你是对的。我更正了进口。希望from tkinter.constants import *from tkinter import * 更好。我可以交替使用from tkinter import constants,并相应地引用常量。另外,time 是在不需要的时候导入的,所以我也删除了它。
    【解决方案2】:

    这很简单,只需在创建ttk.Label 时使用textvariable= 选项而不是text= 选项,然后Label 将在每次vegtable StringVar 的值更改时自动更新。

    #    ttk.Label(frame, text=value).grid(column=1, row=2)
        ttk.Label(frame, textvariable=vegetable).grid(column=1, row=2)
        ttk.Label(frame, text='PH Level').grid(column=0, row=2)
    

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2011-04-09
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多