2015-12-12 36 views
0

我正在使用tkinter在python中創建一個簡單的溫度轉換器。我創建了一個帶有選項和轉換按鈕的下拉菜單。我想要做的是當下拉菜單的更改我希望按鈕做不同的事情。我怎樣才能做到這一點?[Python]根據下拉菜單選項更新GUI

(例如在這種情況下:如果攝氏華氏選擇按鈕應該轉換CEL如果法爾至攝氏選擇應該這樣轉換爲華氏。)

下面是代碼:

from tkinter import * 

def converter(): 
    # Create functions for conversion 
    def cel_fahr(): 
     res = int(entry.get()) * 9/5 +32 
     print (res) 
    def fahr_cel(): 
     res = (int(entry.get()) - 32) * 5/9 
     print (res) 

    #Options list for the dropdown 
    list_opt = ['Celsius to Fahrenheit', 'Fahrenheit to Celsius'] 
    # Create the main window 
    root = Tk() 
    # Rename the title of the window  
    root.title("Temperature Converter") 
    # Set the size of the window 
    root.geometry("250x250") 
    # Set resizable FALSE 
    root.resizable(0,0) 
    # Create a variable for the default dropdown option 
    var1 = StringVar() 
    # Set the default drop down option 
    var1.set(list_opt[0]) 
    # Create the dropdown menu 
    dropdown = OptionMenu(root, var1, *list_opt) 
    dropdown.configure(state="active") 
    # Place the dropdown menu 
    dropdown.place(x=45, y=10) 

    # Create an entry 
    entry = Entry(root) 
    entry.place (x=47, y=60) 

    #Create a button 
    button = Button(root, text='Convert', command=cel_fahr) 
    button.place(x=85,y=90) 

    #I TRIED THIS BUT NO    
    #if var1 == list_opt[0]: 
    #button = Button(root, text='Convert', command=cel_fahr) 
    #button.place(x=85,y=90) 
    #if var1 == list_opt[1]: 
    #button = Button(root, text='Convert', command=fahr_cel) 
    #button.place(x=85,y=90) 


root.mainloop() 



converter() 

回答

1

交換你的代碼一點點:

from tkinter import * 

def converter(): 
    # Create functions for conversion 
    def cel_fahr(): 
     res = int(entry.get()) * 9/5 +32 
     print (res) 
    def fahr_cel(): 
     res = (int(entry.get()) - 32) * 5/9 
     print (res) 

    def convert(): 
     if selected.get() == 'Celsius to Fahrenheit': 
      cel_fahr() 
     else: 
      fahr_cel() 

    #Options list for the dropdown 
    list_opt = ['Celsius to Fahrenheit', 'Fahrenheit to Celsius'] 
    # Create the main window 
    root = Tk() 
    # Rename the title of the window  
    root.title("Temperature Converter") 
    # Set the size of the window 
    root.geometry("250x250") 
    # Set resizable FALSE 
    root.resizable(0,0) 
    # Create a variable for the default dropdown option 
    selected = StringVar(root) 
    # Set the default drop down option 
    selected.set('Celsius to Fahrenheit') 
    # Create the dropdown menu 
    dropdown = OptionMenu(root, selected, 'Celsius to Fahrenheit', 'Fahrenheit to Celsius') 
    # Place the dropdown menu 
    dropdown.place(x=45, y=10) 

    # Create an entry 
    entry = Entry(root) 
    entry.place (x=47, y=60) 

    #Create a button 
    button = Button(root, text='Convert', command=convert) 
    button.place(x=85,y=90) 

    root.mainloop() 



converter() 

相反的選項列表之中,我剛把他們進入菜單在創建時。當按下按鈕時,它會根據下拉菜單中選擇的值調用決定使用哪種轉換的函數。

我也更改了var1的變量名稱(「選中」),因爲它不是非常具有描述性,並且讓代碼變得有點混亂。

相關問題