Вернуть значение с помощью OptionMenu - PullRequest
1 голос
/ 25 июня 2019

Я хочу вернуть значение функцией option_changed и сохранить ее вне функции, которую я пытался использовать с помощью глобальной переменной, но она не работает. Я использую python 3.7.3 и Tkinter GUI

def option_changed(*args):
    global en
    en=format(variable.get())
    print(format(variable.get()))
    #return format(variable.get())
    print("Inside fun"+en)
# making data frame
data1 = pd.read_csv("ver.csv")
print("data", data1.columns)
variable = StringVar(root)
variable.set("-select-") # default value
variable.trace("w", option_changed) # trace value
w = OptionMenu(root, variable, *data1.columns).pack()
#print(en)
mainloop()

1 Ответ

0 голосов
/ 25 июня 2019

Вы можете использовать функцию return

def option_changed(*args):
    en=format(variable.get())
    print(format(variable.get()))
    #return format(variable.get())
    print("Inside fun"+en)
    return en #<--- With this you return the variable
# making data frame
output_variable = option_changed() #<--- With this line you save the output of the option_changed() method into the variable en
data1 = pd.read_csv("ver.csv")
print("data", data1.columns)
variable = StringVar(root)
variable.set("-select-") # default value
variable.trace("w", option_changed()) # trace value #<--- What you probably wanted was this, you were missing the paranthese here.
w = OptionMenu(root, variable, *data1.columns).pack()
print(output_variable) #<--- Now you can use this variable 
mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...