通过OptionMenu返回值

时间:2019-06-25 06:56:14

标签: python tkinter

我想通过函数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 :(得分:0)

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

   List<dynamic> items = snapshot.data.catlist.sublist(0, 7); // Take first 7 items from the list

   // Add a new item to the end, use the format as for other items, if needed
   items.add({
       'imagePath': 'http://myimage.url',
       ....
   });