我正在尝试根据他们在“放置”中选择的内容来更新“ label_enter_what”中的标签文本。因此,如果他们选择“能量”,则标签将更改为:例如,“在下面的所选单位中输入波长”。对不起,如果代码看起来凌乱,这是我第一次编码。这应该是我所使用的光子性质计算器之所以取笑是因为在物理学中我们目前正在这样做,但是要用笔和计算器。
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()
答案 0 :(得分:0)
您可以使用
tk.OptionMenu( ... command=function)
运行功能,该功能将获取选定的值,并将更改标签中的文本
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()