2017-06-05 52 views
1

我做一個簡單的GUI,我的目標現在是爲用戶選擇一個選項(運動或電力),然後他們就按下按鈕以進入他們選擇的一個新的屏幕。目前,無論選擇哪個按鈕,該按鈕都會執行相同的操作,而且我不知道如何更改該按鈕。我正在使用Python 3.6.1使用Python與Tkinter的,我怎樣才能使按下一個按鈕做不同的事情,這取決於選項,在選項菜單中選擇?

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() 

回答

2

沒有什麼特別的你需要做的。在回調中,您可以獲取選項菜單的值,然後執行適當的操作。

def callback1(): 
    if om1v.get() == "Kinematics": 
     do_kinematics 
    else: 
     do_electricity() 
相關問題