我正在开发一个简单的程序,我需要从Combobox
。当Combobox
位于第一个创建的窗口中,但例如如果我有两个窗口并且Combobox
在第二个中,我无法从中读取值。
例如 :
from tkinter import *
from tkinter import ttk
def comando():
print(box_value.get())
parent = Tk() #first created window
ciao=Tk() #second created window
box_value=StringVar()
coltbox = ttk.Combobox(ciao, textvariable=box_value, state='readonly')
coltbox["values"] = ["prova","ciao","come","stai"]
coltbox.current(0)
coltbox.grid(row=0)
Button(ciao,text="Salva", command=comando, width=20).grid(row=1)
mainloop()
如果我更改小部件的父级ciao
给父母它有效!
谁能给我解释一下吗?
你不能有两个Tk()
视窗。一个必须是Toplevel
.
要获取变量,你可以这样做box_value.get()
下拉框示例:
class TableDropDown(ttk.Combobox):
def __init__(self, parent):
self.current_table = tk.StringVar() # create variable for table
ttk.Combobox.__init__(self, parent)# init widget
self.config(textvariable = self.current_table, state = "readonly", values = ["Customers", "Pets", "Invoices", "Prices"])
self.current(0) # index of values for current table
self.place(x = 50, y = 50, anchor = "w") # place drop down box
print(self.current_table.get())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)