python 在 for 循环中创建的组合框中的 tkinter 参考
python tkinter reference in comboboxes created in for loop
到目前为止我的代码,我想根据 combobox 和 selection 导入图像。我需要参考 self.box。例如self.box1、self.box2 等,或者如果可能的话将它们附加到循环中的某处
from Tkinter import *
import ttk
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
self.master = master
self.grid()
self.create_widgets()
def create_widgets(self):
for i in range(9):
self.box = ttk.Combobox(self, state="readonly")
self.box["values"] = ("apple", "bannana", "cherry", "raspberry", "blueberry", "lemon", "tomato", "potato",
"None")
self.box.grid(row=1+i, column=2, pady=1, padx=1, sticky=E+W+N+S)
self.box.current(i)
self.box.bind("<<ComboboxSelected>>", self.change_icon)
print self.box["values"][i]
def change_icon(self, event):
self.var_Selected = self.box.current()
print "The user selected value now is:"
print self.var_Selected
root = Tk()
root.title("Random title")
root.geometry("500x250")
app = Application(root)
root.mainloop()
您可以让您的应用程序保留一个字典对象(或一个列表,但实现高度依赖于索引),将您的框存储在以 i
作为键的字典中:
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
# Various initialization code here
self.box_dict = {}
def create_widgets(self):
for i in range(9):
box = ttk.Combobox(self, state="readonly")
# Do various things with your box object here
self.box_dict[i] = box
# Only complication is registering the callback
box.bind("<<ComboboxSelected>>",
lambda event, i=i: self.change_icon(event, i))
def change_icon(self, event, i):
self.var_Selected = self.box_dict[i].current()
print "The user selected value now is:"
print self.var_Selected
然后通过self.box_dict[0]
等访问这些框
Edit 我更新了 bind
和 change_icon
方法,让每个框在事件发生时将其索引号发送到 change_icon
触发。
Edit2 将实现更改为使用 dict
而不是 list
,这似乎更可靠。
对于那些寻找更明确的解决方案的人来说,这里有一个完全有效的解决方案 (Python 3.0+)...记住孩子们,索引从 0 开始。
from tkinter import *
from tkinter import ttk
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
self.master = master
self.grid()
self.box_dict = {}
def create_widgets():
for i in range(9):
box = ttk.Combobox(self, state="readonly")
box["values"] = (
"None",
"Apple",
"Bannana",
"Cherry",
"Raspberry",
"Blueberry",
"Lemon",
"Tomato",
"Potato"
)
box.grid(row=1 + i, column=2, pady=1, padx=1, sticky=E + W + N + S)
box.current(0)
self.box_dict[i] = box
box.bind("<<ComboboxSelected>>",
lambda event, i=i: change_icon(event, i))
print(f"Option {i} is:",box["values"][i])
def change_icon(event, i):
var_Selected = self.box_dict[i].current()
print(f"The user selected of box {i} is:", var_Selected)
create_widgets()
root = Tk()
root.title("Random title")
root.geometry("500x250")
app = Application(root)
root.mainloop()
到目前为止我的代码,我想根据 combobox 和 selection 导入图像。我需要参考 self.box。例如self.box1、self.box2 等,或者如果可能的话将它们附加到循环中的某处
from Tkinter import *
import ttk
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
self.master = master
self.grid()
self.create_widgets()
def create_widgets(self):
for i in range(9):
self.box = ttk.Combobox(self, state="readonly")
self.box["values"] = ("apple", "bannana", "cherry", "raspberry", "blueberry", "lemon", "tomato", "potato",
"None")
self.box.grid(row=1+i, column=2, pady=1, padx=1, sticky=E+W+N+S)
self.box.current(i)
self.box.bind("<<ComboboxSelected>>", self.change_icon)
print self.box["values"][i]
def change_icon(self, event):
self.var_Selected = self.box.current()
print "The user selected value now is:"
print self.var_Selected
root = Tk()
root.title("Random title")
root.geometry("500x250")
app = Application(root)
root.mainloop()
您可以让您的应用程序保留一个字典对象(或一个列表,但实现高度依赖于索引),将您的框存储在以 i
作为键的字典中:
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
# Various initialization code here
self.box_dict = {}
def create_widgets(self):
for i in range(9):
box = ttk.Combobox(self, state="readonly")
# Do various things with your box object here
self.box_dict[i] = box
# Only complication is registering the callback
box.bind("<<ComboboxSelected>>",
lambda event, i=i: self.change_icon(event, i))
def change_icon(self, event, i):
self.var_Selected = self.box_dict[i].current()
print "The user selected value now is:"
print self.var_Selected
然后通过self.box_dict[0]
等访问这些框
Edit 我更新了 bind
和 change_icon
方法,让每个框在事件发生时将其索引号发送到 change_icon
触发。
Edit2 将实现更改为使用 dict
而不是 list
,这似乎更可靠。
对于那些寻找更明确的解决方案的人来说,这里有一个完全有效的解决方案 (Python 3.0+)...记住孩子们,索引从 0 开始。
from tkinter import *
from tkinter import ttk
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master, relief="sunken", border=1)
self.master = master
self.grid()
self.box_dict = {}
def create_widgets():
for i in range(9):
box = ttk.Combobox(self, state="readonly")
box["values"] = (
"None",
"Apple",
"Bannana",
"Cherry",
"Raspberry",
"Blueberry",
"Lemon",
"Tomato",
"Potato"
)
box.grid(row=1 + i, column=2, pady=1, padx=1, sticky=E + W + N + S)
box.current(0)
self.box_dict[i] = box
box.bind("<<ComboboxSelected>>",
lambda event, i=i: change_icon(event, i))
print(f"Option {i} is:",box["values"][i])
def change_icon(event, i):
var_Selected = self.box_dict[i].current()
print(f"The user selected of box {i} is:", var_Selected)
create_widgets()
root = Tk()
root.title("Random title")
root.geometry("500x250")
app = Application(root)
root.mainloop()