简体   繁体   English

Tkinter 更新标签文本

[英]Tkinter update label text

I am trying to update the label text in "label_enter_what" in accordance to what they chose in "drop".我正在尝试根据他们在“drop”中选择的内容更新“label_enter_what”中的标签文本。 So if they choose "Energy", the label would change to: Enter wavelength in chosen unit below", for example. Sorry if the code looks messy, it's my first time coding. This is supposed to be a photon property calculator that i am making for fun because in physics we are currently doing this, but with pens and calculators.因此,如果他们选择“能量”,标签将更改为:在下面选择的单位中输入波长“,例如。对不起,如果代码看起来很乱,这是我第一次编码。这应该是我的光子属性计算器取笑是因为在物理学中,我们目前正在这样做,但是使用钢笔和计算器。

import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
HEIGHT = 600
WIDTH = 900

root = tk.Tk()

root.title("Photon property calculator")

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

background_image = ImageTk.PhotoImage(Image.open('interference.jpg'))
background_label = tk.Label(root, image=background_image)
background_label.place(relheight=1, relwidth=1)

frame = tk.Frame(root, bg='#3E3E3E', bd=5)
frame.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.75, anchor='n')

frame_upper = tk.Frame(root, bg='#3E3E3E', bd=5)
frame_upper.place(relx=0.5, rely=0.03, relheight=0.06, relwidth=0.75, anchor='n')

label_what_to_calc = tk.Label(frame_upper, bg='white', text='Enter what \n to calculate')
label_what_to_calc.place(relx=0, rely=0, relheight=1, relwidth=0.15)

label_enter_what = tk.Label(frame_upper, bg='white', text='Enter * in a chosen unit below.')
label_enter_what.place(relx=0.2, relheight=1, relwidth=0.2)

label_unit = tk.Label(frame_upper, bg='white', text='')
label_unit.place(relx=0.5, relheight=1, relwidth=0.1)

lower_frame = tk.Frame(root, bg='#60A8FF', bd=10)
lower_frame.place(relx=0.5, rely=0.3, relheight=0.5, relwidth=0.75, anchor='n')

entry_value = tk.Entry(frame, font=40, bg='white')
entry_value.place(relx=0.175, rely=0, relheight=1, relwidth=0.4)

OPTIONS = [
    "Energy",
    "Frequency",
    "Wavelength"
]

clicked = StringVar()
clicked.set(OPTIONS[0])

drop = tk.OptionMenu(frame, clicked, *OPTIONS)
drop.place(relx=0, rely=0, relheight=1, relwidth=0.15)

OPTIONS_UNITS = ["μm",
                 "nm",
                 "pm",
                 "aJ",
                 "zJ",

]

clicked_1 = StringVar()
clicked_1.set(OPTIONS_UNITS[1])

drop_units = tk.OptionMenu(frame, clicked_1, *OPTIONS_UNITS)
drop_units.place(relx=0.6, rely=0, relheight=1, relwidth=0.09)

button = tk.Button(frame, text='Calculate!', font=40, bg="#F96612", fg='black')
button.place(relx=0.7, relheight=1, relwidth=0.3)

label = tk.Label(lower_frame, bg='white', text=clicked.get())
label.place(relheight=1, relwidth=1)

root.mainloop()

You can use您可以使用

tk.OptionMenu( ... command=function)

to run function which will get selected value and it will change text in label运行将获得选定值的函数,它将更改标签中的文本


import tkinter as tk

# --- functions ---

def on_select(value):
    label['text'] = value

# --- main ---

root = tk.Tk()

label = tk.Label(root, text='?')
label.pack()

OPTIONS = ["Energy", "Frequency", "Wavelength"]
value_var = tk.StringVar(value=OPTIONS[0])

op = tk.OptionMenu(root, value_var, *OPTIONS, command=on_select)
op.pack()

root.mainloop()   

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

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