gpt4 book ai didi

python - 将 Python 与 Tkinter 结合使用,如何根据选项菜单中选择的选项使按钮按下执行不同的操作?

转载 作者:行者123 更新时间:2023-12-01 02:54:21 25 4
gpt4 key购买 nike

我正在制作一个简单的 GUI,我现在的目标是让用户选择一个选项(运动学或电力),然后他们按下按钮转到他们选择的新屏幕。目前,无论选择哪个按钮,该按钮都会执行相同的操作,我不知道如何更改它。我正在使用 Python 3.6.1

from tkinter import *
import tkinter.font

bg_color1 = "#008B8B"

abc = Tk()

abc.title("Physics Problem Solver")
abc.rowconfigure(0, weight=1)
abc.columnconfigure(0, weight=1)

helvetica_bold_16 = tkinter.font.Font(
root = abc,
family="Helvetica",
weight="bold",
size=16)

helvetica_bold_12 = tkinter.font.Font(
root = abc,
family="Helvetica",
weight="bold",
size=12)

app = Frame(abc,
bd=6,
relief="groove",
bg=bg_color1)
app.rowconfigure(0, weight=1)
app.columnconfigure(0, weight=1)
app.grid(sticky=N+S+E+W)

msg1 = Message(app,
text = "Welcome to the Physics Problem Solver!",
font=helvetica_bold_16,
bg=bg_color1,
fg="white",
justify="center",
relief="flat")
msg1.grid(pady=15)

def callback1():
toplevel = Toplevel()
toplevel.title("Window 2")
toplevel.focus_set()

optionList = ("Kinematics",
"Electricity")
om1v= StringVar()
om1v.set(optionList[0])

om1 = OptionMenu(app,
om1v,
"Kinematics",
"Electricity")
om1.grid(pady=20)

b1= Button(app,
text="Go!",
width=5,
activebackground="#007070",
activeforeground="#00ACAC",
fg="black",
justify="center",
font=helvetica_bold_12,
relief="raised",
command=callback1)
b1.grid(pady=20)

abc.mainloop()

最佳答案

您无需做任何特别的事情。在回调中,您可以获得选项菜单的值,然后执行适当的操作。

def callback1():
if om1v.get() == "Kinematics":
do_kinematics
else:
do_electricity()

关于python - 将 Python 与 Tkinter 结合使用,如何根据选项菜单中选择的选项使按钮按下执行不同的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44376791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com