Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用组合框选择字符串,并存储在Tkinter窗口关闭后要使用的变量中_Python_Tkinter_Combobox - Fatal编程技术网

Python 使用组合框选择字符串,并存储在Tkinter窗口关闭后要使用的变量中

Python 使用组合框选择字符串,并存储在Tkinter窗口关闭后要使用的变量中,python,tkinter,combobox,Python,Tkinter,Combobox,我创建了一个简单的窗口,用户可以使用组合框输入一些字符串 我已经成功创建了下拉列表和一个关闭窗口的按钮,但在尝试将所选值存储到变量中以供程序下一部分使用时遇到问题 # start programme window root = tk.Tk() root.title('Great Britain Basketball') root.geometry('800x449+300+130') root.configure(bg='#072462') #def variable and store ba

我创建了一个简单的窗口,用户可以使用组合框输入一些字符串

我已经成功创建了下拉列表和一个关闭窗口的按钮,但在尝试将所选值存储到变量中以供程序下一部分使用时遇到问题

# start programme window
root = tk.Tk()
root.title('Great Britain Basketball')
root.geometry('800x449+300+130')
root.configure(bg='#072462')

#def variable and store based on selection
def comboclick(event):
    select_sheet = cb.get()

#create combobox
cb = ttk.Combobox(root, value=('Mon', 'Tues', 'Wed', 'Thurs'))
cb.current(0)
cb.bind('<<ComboboxSelected>>', comboclick)
cb.pack()

#set close window button
button_close = Button(root, width=35, text='Close Programme', command=root.quit, 
                      fg='#C51E42', bg='#B4B5B4', borderwidth=1).pack()

root.mainloop()

print(select_sheet)
我将select_sheed声明为一个全局变量,因此它可以在函数内部修改,并且我还插入了一个开始值来select_sheet,因此如果用户不更改组合框的值,他仍然会得到一个值

这些都是为了让它工作而必须做出的最微小的改变

将tkinter作为tk导入 从tkinter导入ttk root=tk.tk 根。标题“大不列颠篮球” 根。几何图形“800x449+300+130” root.configurebg='072462' def变量和基于选择的存储 def comboclickevent: 全局选择工作表设置将工作表选择为全局,以便可以对其进行修改 选择_sheet=cb.get 我在这里设置了相同的cb.current值,所以如果用户不更改它,您仍然会得到一个输出。 选择工作表='Mon' 创建组合框 cb=ttk.comboxroot,value='Mon','Tues','Wed','Thurs' cb.0 cb.bind,单击组合键 包装 设置关闭窗口按钮 按钮关闭=tk.Buttonroot,宽度=35,text='close program',command=root.quit, fg='C51E42',bg='B4B4',边框宽度=1 root.mainloop 打印选择表
comboclikc回调函数中select_sheet的赋值是对一个局部变量的赋值,因此首先需要将其声明为全局变量。离题,但也请注意,按钮_close将被分配从pack返回的值,该值始终为空。感谢您的反馈和指导,让我感觉到您Eric,已经盯着它看了很久了,但更改已经完成了工作!感谢您的解释,因为这样做更有意义