获取 python 中的组合框值
Get combobox value in python
我正在开发一个简单的程序,我需要从 Combobox
中获取值。当 Combobox
在第一个创建的 window 中时很容易,但是例如,如果我有两个 windows 而 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()
windows。一个必须是 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())
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
root = Tk()
root.geometry("400x400")
# Length and width window :D
cmb = ttk.Combobox(root, width="10", values=("prova","ciao","come","stai"))
# to create checkbox
# cmb = Combobox
#now we create simple function to check what user select value from checkbox
def checkcmbo():
if cmb.get() == "prova":
messagebox.showinfo("What user choose", "you choose prova")
# if user select prova show this message
elif cmb.get() == "ciao":
messagebox.showinfo("What user choose", "you choose ciao")
# if user select ciao show this message
elif cmb.get() == "come":
messagebox.showinfo("What user choose", "you choose come")
elif cmb.get() == "stai":
messagebox.showinfo("What user choose", "you choose stai")
elif cmb.get() == "":
messagebox.showinfo("nothing to show!", "you have to be choose something")
cmb.place(relx="0.1",rely="0.1")
btn = ttk.Button(root, text="Get Value",command=checkcmbo)
btn.place(relx="0.5",rely="0.1")
root.mainloop()
我正在开发一个简单的程序,我需要从 Combobox
中获取值。当 Combobox
在第一个创建的 window 中时很容易,但是例如,如果我有两个 windows 而 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()
windows。一个必须是 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())
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
root = Tk()
root.geometry("400x400")
# Length and width window :D
cmb = ttk.Combobox(root, width="10", values=("prova","ciao","come","stai"))
# to create checkbox
# cmb = Combobox
#now we create simple function to check what user select value from checkbox
def checkcmbo():
if cmb.get() == "prova":
messagebox.showinfo("What user choose", "you choose prova")
# if user select prova show this message
elif cmb.get() == "ciao":
messagebox.showinfo("What user choose", "you choose ciao")
# if user select ciao show this message
elif cmb.get() == "come":
messagebox.showinfo("What user choose", "you choose come")
elif cmb.get() == "stai":
messagebox.showinfo("What user choose", "you choose stai")
elif cmb.get() == "":
messagebox.showinfo("nothing to show!", "you have to be choose something")
cmb.place(relx="0.1",rely="0.1")
btn = ttk.Button(root, text="Get Value",command=checkcmbo)
btn.place(relx="0.5",rely="0.1")
root.mainloop()