Python和Tkinter:类和实例创建错误

时间:2018-10-18 09:56:08

标签: python tkinter

我有两个类,正在尝试用命令创建一个按钮来调用另一个类中的方法。由于出现错误,我目前在创建实例时遇到问题:

AttributeError: 'Queue' object has no attribute 'number_queue' 

这是Widgets和Widgets方法中引用的变量。

任何指针都将受到欢迎,我一直在努力使用实例和跨类引用。我的主要问题之一是是否创建了如下实例:

instance1 = Widgets(self,master,queue_type, symbol, examples)

我要在实例变量中输入什么? 而我需要在Queue类中添加什么。

这是带有按钮引用小部件的类

class Queue(tk.Frame):
    """A Tkinter grid"""

    def __init__(self, master, columns, *args, **kwargs):
        """
        Construct a new grid

        Parameters:
            master (tk.Tk|tk.Frame): Frame containing this widget
            columns (int): Amount of columns in the grid
        """
        super().__init__(master, *args, **kwargs)

        self._row = 0
        #self._callback = Widgets.remove_student_tick
        ##self.awidget = Widgets()
        # configure the grid to fill the space
        for column in range(columns):
            tk.Grid.columnconfigure(self, column, weight=1)

    def add_student_row(self, first_last, number,questions_asked):

        column1= number
        column2 = first_last
        column3 = questions_asked
        column4 = ''
        column5 = ''
        values= [column1, column2, column3, column4, column5]

        for column, value in enumerate(values):
            label = tk.Label(self, text=value)
            label.grid(row=self._row, column=column, sticky=tk.W, ipadx = 10)

        self._row += 1


        cross = Button(self, bg='red', command =Widgets.remove_student_tick(self, first_last)
        cross.grid(column = 5)

这是我正在引用的类和方法。

class Widgets(tk.Frame):
    def __init__(self,master,queue_type, symbol, examples, **kwargs):
        super().__init__(master, **kwargs)
    ***some irrelavant code****
    def remove_student_tick(self, first_last):
    ***some irrelavant code***

任何帮助将不胜感激。

编辑: 追溯:

    Traceback (most recent call last):
      File "C:\Users\AlexLaptop\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args) 
   File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 77, in callback
    self._grid.add_student_row(first_last,number, self._q_student_queue[first_last])
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 209, in add_student_row
    cross = Button(self, bg='red', command = Widgets.remove_student_tick(self, first_last))
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 86, in remove_student_tick
    index = self.number_queue.index(first_last)
AttributeError: 'Queue' object has no attribute 'number_queue'
{'asd': 0, 'aasd': 0}
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\AlexLaptop\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 77, in callback
    number = self.number_queue.index(first_last) + 1
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 209, in add_student_row
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 86, in remove_student_tick
    def remove_student_tick(self, first_last):
AttributeError: 'Queue' object has no attribute 'number_queue'

使用小部件而不是框架:

File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 227, in main
    app = App(root)
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 126, in __init__
    quick_frame = Widgets(quickFrame, "Quick", "<", quick_examples)
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 48, in __init__
    self._grid = Queue(self, 4)
  File "C:\Users\AlexLaptop\Desktop\CSSE A3\GUI.py", line 172, in __init__
    super().__init__(master, *args, **kwargs)
TypeError: __init__() missing 3 required positional arguments: 'queue_type', 'symbol', and 'examples'

0 个答案:

没有答案