代码段:
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
class BirdsEye(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Hello Tkinter")
self.geometry("480x320")
self.resizable(width = False, height = False)
FeatureExtractionView(self).grid(sticky=(tk.E + tk.W + tk.N + tk.S))
class FeatureExtractionView(tk.Frame):
def __init__(self, parent = None, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.parent = parent
self.object_list = ['human', 'cat']
self.draw_widgets(parent)
def get_object_list(self):
return self.object_list
def draw_widgets(self, parent):
# Parent container to hold widgets for parameter selection
container = ttk.LabelFrame(parent, text = "Feature Extraction Parameters")
container.grid(row = 0, column = 0, padx = 5, pady = 5)
parent.rowconfigure(0, weight = 1)
parent.columnconfigure(0, weight = 1)
object_combo = ttk.Combobox(container, self.get_object_list())
object_combo.grid(row = 1, column = 0)
container.rowconfigure(1, weight = 1)
container.columnconfigure(0, weight = 1)
堆栈跟踪:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-144-8f8ef2b5022b> in <module>
----> 1 app = BirdsEye()
2 app.mainloop()
<ipython-input-141-cead3bb4d34c> in __init__(self, *args, **kwargs)
5 self.geometry("480x320")
6 self.resizable(width = False, height = False)
----> 7 FeatureExtractionView(self).grid(sticky=(tk.E + tk.W + tk.N + tk.S))
<ipython-input-143-b47db92b9d00> in __init__(self, parent, *args, **kwargs)
6 self.parent = parent
7 self.object_list = ['human', 'cat']
----> 8 self.draw_widgets(parent)
9
10 def get_object_list(self):
<ipython-input-143-b47db92b9d00> in draw_widgets(self, parent)
18 parent.columnconfigure(0, weight = 1)
19
---> 20 object_combo = ttk.Combobox(container, self.get_object_list())
21 object_combo.grid(row = 1, column = 0)
22 container.rowconfigure(1, weight = 1)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
我正在为一个大学项目制作一个小的UI。该视图需要有一个带有值列表的组合框。我正在尝试通过_getter方法将成员变量object_list传递给LabelFrame小部件中的组合框。但是,它不起作用。另外,如果有人有足够的资源进行tkinter,请提供相同的资源。
更新: 添加了堆栈跟踪。
答案 0 :(得分:1)
您需要使用values
选项指定组合框的值:
object_combo = ttk.Combobox(container, values=self.get_object_list())