gpt4 book ai didi

python - 通过OptionMenu返回值

转载 作者:太空宇宙 更新时间:2023-11-03 20:43:09 24 4
gpt4 key购买 nike

我想通过函数 option_changed 返回值并将其存储在函数外部我尝试过使用全局变量,但它不起作用我正在使用 python 3.7.3Tkinter 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()

最佳答案

您可以使用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()

关于python - 通过OptionMenu返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56748391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com