[Python]根据下拉菜单选项

时间:2015-12-12 18:16:35

标签: python user-interface tkinter

我正在使用tkinter在python中创建一个简单的温度转换器。我创建了一个带有选项和转换按钮的下拉菜单。我想要做的是当下拉菜单的更改我希望按钮做一个不同的事情。我怎样才能做到这一点?

(在这种情况下的示例:如果选择了celcius到fahrenheit按钮应该将cel转换为华氏度,如果选择fahr到摄氏度,它应该转换为那种方式。)

以下是代码:

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 个答案:

答案 0 :(得分:2)

稍微调整你的代码:

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的变量名称(到"选择"),因为它不是很具描述性,并且编码有点令人困惑。