我正在尝试将类变量值添加到函数内的列表中,但我没有看到任何错误或预期的输出?当我取消注释列表代码时,组合框不会显示。
在函数之外,此代码可以独立工作:
value_list = []
selected_float = 0.5
value_list.append(selected_float)
print('The value_list value is: ')
print(value_list)
输出符合预期: value_list 值为: [0.5]
但是,这里的代码具有不打印任何内容的功能。我必须注释掉 1. 和 2. 处的代码,否则它会停止工作并且不会显示组合框。
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Create a function to clear the combobox
def clear_cb(self):
self.cb.set('')
def handle_selection(self):
selected_index = self.cb.current() # Get the index of the selected item
print(selected_index)
selected_tuple = self.data[selected_index] # Get the selected tuple
print(selected_tuple)
selected_float = float(selected_tuple[-1]) # Extract the float value from the tuple
print(selected_float) # Print the extracted float value
# 2. Commented out these lines:
#self.value_list.append(selected_float)
#print('The value_list value is: ')
#print(self.value_list)
class ComboWidget():
def __init__(self):
# Create a combobox widget
self.var = StringVar()
self.cb = ttk.Combobox(win, textvariable= self.var)
self.cb['values'] = self.data
self.cb['state'] = 'readonly'
self.cb.pack(fill = 'x',padx=5, pady=5)
# Define Tuple
self.data = [('First', 'F', '0.5'), ('Next', 'N', '1.0'), ('Middle', 'M', '0.6'), ('Last', 'L', '0.24')]
# 1. Commented out the declaration.
#self.value_list = []
self.cb.bind("<<ComboboxSelected>>", handle_selection)
# Create a button to clear the selected combobox text value
self.button = Button(win, text= "Clear", command= clear_cb)
self.button.pack()
win.mainloop()
我相信应该可以将实例变量和类变量混合在一起,但我所做的事情是错误的?有什么想法吗?
我不是 Tkinter 的大用户,但我过去已经与它进行了足够多的斗争,以使您的代码正常运行!
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
class ComboWidget():
def __init__(self):
# Create a combobox widget
self.var = StringVar()
self.cb = ttk.Combobox(win, textvariable= self.var)
# Define Tuple
self.data = [('First', 'F', '0.5'), ('Next', 'N', '1.0'), ('Middle', 'M', '0.6'), ('Last', 'L', '0.24')]
self.cb['values'] = self.data
self.cb['state'] = 'readonly'
self.cb.pack(fill = 'x',padx=5, pady=5)
# 1. Commented out the declaration.
self.value_list = []
self.cb.bind("<<ComboboxSelected>>", self.handle_selection)
# Create a button to clear the selected combobox text value
self.button = Button(win, text= "Clear", command= self.clear_cb)
self.button.pack()
# Create a function to clear the combobox
def clear_cb(self):
self.cb.set('')
def handle_selection(self, _):
selected_index = self.cb.current() # Get the index of the selected item
print(selected_index)
selected_tuple = self.data[selected_index] # Get the selected tuple
print(selected_tuple)
selected_float = float(selected_tuple[-1]) # Extract the float value from the tuple
print(selected_float) # Print the extracted float value
# 2. Commented out these lines:
self.value_list.append(selected_float)
print('The value_list value is: ')
print(self.value_list)
c=ComboWidget()
win.mainloop()
它仍然需要一些整理,但我希望这能让你回到正轨。以下是一些问题:
正如@meshkati 评论的那样,你实际上必须实例化 ComboWidget。
正如 @OneCricketeer 评论的那样,您定义的两个函数实际上是作为 ComboWidget 的方法编写的,因此属于类的主体。
您对 self.data 的定义需要在首次使用之前
handle_selection 和clear_cb 需要添加“self”前缀。它们与方法绑定在一起
self.handle_selection方法应该有2个参数:self(因为它是一个方法)和一个事件。因为你实际上并没有使用传入的事件,所以你可以只输入'_'
我想就是这样 - 祝你这个项目的进一步发展好运!