【问题标题】:Error: __init__() takes from 1 to 2 positional arguments but 3 were given (Python: tkinter)错误:__init__() 采用 1 到 2 个位置参数,但给出了 3 个(Python:tkinter)
【发布时间】:2019-10-17 15:14:59
【问题描述】:

代码片段:

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 有很好的资源,请提供相同的资源。

更新: 添加了堆栈跟踪。

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    您需要使用values 选项指定组合框的值:

    object_combo = ttk.Combobox(container, values=self.get_object_list())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 2022-10-06
      • 2023-03-15
      • 1970-01-01
      • 2022-01-04
      • 2021-01-16
      • 1970-01-01
      相关资源
      最近更新 更多