如何将类变量值添加到列表中,python 3x?
我正在尝试在一个函数里把类的变量值添加到一个列表中,但我没有看到任何错误,也没有得到预期的输出?当我取消注释列表的代码时,组合框就不显示了。
在函数外部,这段代码可以单独运行:
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()
我觉得应该可以把实例变量和类变量混合在一起,但我做的某些事情是错的?有什么想法吗?
1 个回答
1
我不是一个经常使用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方法应该有两个参数:self,因为这是一个方法,还有一个事件。因为你实际上并没有使用传入的事件,所以可以用'_'代替。
我想就这些了,祝你在这个项目的进一步开发中好运!