简体   繁体   中英

only proceed when function is called

The user should pick a matter from the list in the dropdown menu. Then it would give back a schematic drawing of that matter with the Turtle mudole, i do not include that here. I would want it to wait computing

m = n - 1

after the user's input is registered.

from tkinter import *

OPTIONS = ['methaan','ethaan','propaan','butaan']


master = Tk()

variable = StringVar(master)
variable.set(OPTIONS[0]) # default value

w = OptionMenu(master, variable, *OPTIONS)
w.pack()

def ok():
    variable.get()
    if variable == 'methaan':
        n = 1
    elif variable == 'ethaan':
        n = 2
    elif variable == 'propaan':
        n = 3
    elif variable == 'butaan':
        n = 4


button = Button(master, text="OK", command=ok)
button.pack()

m = n - 1
print(m)
mainloop()

It gives back that n is not defined.

Desired outcome

# 0 to 3 depending on what matter you choose.

Thank you in advance.

Move the last 2 lines within the function itself, because they won't get executed if they are under mainloop .

Here is the full code:

from tkinter import *

OPTIONS = ['methaan','ethaan','propaan','butaan']


master = Tk()

variable = StringVar(master)
variable.set(OPTIONS[0]) # default value

w = OptionMenu(master, variable, *OPTIONS)
w.pack()

def ok():
    if variable.get() == 'methaan':
        n = 1
    elif variable.get()  == 'ethaan':
        n = 2
    elif variable.get()  == 'propaan':
        n = 3
    elif variable.get()  == 'butaan':
        n = 4
    m = n - 1
    print(m)

button = Button(master, text="OK", command=ok)
button.pack()
mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM