关于类的Python中的AttributeError

时间:2017-06-09 01:21:26

标签: python python-3.x tkinter

import tkinter

DEFAULT_FONT = 'Helvetica, 16'


class UserInterface:
    def __init__(self):
        self._root_window = tkinter.Tk()

        self._canvas = tkinter.Canvas(master=self._root_window,
                                      width=400,
                                      height=400,
                                      background='#2A722E')

        self._canvas.grid(row=0,
                          column=0,
                          padx=30,
                          pady=30,
                          sticky=tkinter.W + tkinter.E + tkinter.N + tkinter.S)

        self._ask_user_input()

    def run(self):
        self._root_window.mainloop()

    def _ask_user_input(self):
        user_input = UserInput()
        user_input.show()

        if user_input.ok_is_clicked():
            input_list = user_input.get_input_list()
            return input_list


class UserInput:
    def __init__(self):
        self._user_input = tkinter.Toplevel()

        row_num = tkinter.Label(master=self._user_input,
                                text='Specify a row number 4-16:',
                                font=DEFAULT_FONT)
        row_num.grid(row=0,
                     column=0,
                     padx=10,
                     pady=10,
                     sticky=tkinter.W)

        self._row_num_entry = tkinter.Entry(master=self._user_input,
                                            width=15,
                                            font=DEFAULT_FONT)
        self._row_num_entry.grid(row=0,
                                 column=1,
                                 padx=10,
                                 pady=10,
                                 sticky=tkinter.E)

        col_num = tkinter.Label(master=self._user_input,
                                text='Specify a column number 4-16:',
                                font=DEFAULT_FONT)
        col_num.grid(row=1,
                     column=0,
                     padx=10,
                     pady=10,
                     sticky=tkinter.W)

        self._col_num_entry = tkinter.Entry(master=self._user_input,
                                            width=15,
                                            font=DEFAULT_FONT)
        self._col_num_entry.grid(row=1,
                                 column=1,
                                 padx=10,
                                 pady=10, sticky=tkinter.E)

        first_player = tkinter.Label(master=self._user_input,
                                     text='Specify the first player B/W:',
                                     font=DEFAULT_FONT)
        first_player.grid(row=2,
                          column=0,
                          padx=10,
                          pady=10,
                          sticky=tkinter.W)

        self._first_player_entry = tkinter.Entry(master=self._user_input,
                                                 width=15,
                                                 font=DEFAULT_FONT)
        self._first_player_entry.grid(row=2,
                                      column=1,
                                      padx=10,
                                      pady=10,
                                      sticky=tkinter.W)

        win_condition = tkinter.Label(master=self._user_input,
                                      text='Specify a winning condition, < or >:',
                                      font=DEFAULT_FONT)
        win_condition.grid(row=3,
                           column=0,
                           padx=10,
                           pady=10,
                           sticky=tkinter.W)

        self._win_condition_entry = tkinter.Entry(master=self._user_input,
                                                  width=15,
                                                  font=DEFAULT_FONT)
        self._win_condition_entry.grid(row=3,
                                       column=1,
                                       padx=10,
                                       pady=10,
                                       sticky=tkinter.E)

        ok_button = tkinter.Button(master=self._user_input,
                                   text='OK',
                                   font=DEFAULT_FONT,
                                   command=self._ok_button_clicked)
        ok_button.grid(row=4,
                       columnspan=2,
                       padx=40,
                       pady=40)

        self._ok_clicked = False
        self._input_list = []

    def show(self) -> None:
        self._user_input.grab_set()
        self._user_input.wait_window()

    def ok_is_clicked(self) -> bool:
        return self._ok_clicked

    def _ok_button_clicked(self):
        self._ok_clicked = True

        row_num = self._row_num_entry.get()
        col_num = self._col_num_entry.get()
        starter = self._first_player_entry.get()
        winning = self._win_condition_entry.get()

        self._input_list.extend([row_num, col_num, starter, winning])

        self._user_input.destroy()

    def get_input_list(self) -> list:
        return self._input_list


if __name__ == '__main__':
    game = UserInterface()
    game.run()

但是当我通过将UserInteface()类分配给变量来运行它时,我收到以下错误:

Traceback (most recent call last):
  File "/Users/cagataycakiroglu/Desktop/othello_gui.py", line 166, in <module>
    game = UserInterface()
  File "/Users/cagataycakiroglu/Desktop/othello_gui.py", line 26, in __init__
    user_input = self._ask_user_input()
  File "/Users/cagataycakiroglu/Desktop/othello_gui.py", line 36, in _ask_user_input
    user_input = UserInput()
  File "/Users/cagataycakiroglu/Desktop/othello_gui.py", line 127, in __init__
    command = self._ok_button_clicked)
AttributeError: 'UserInput' object has no attribute '_ok_button_clicked'

我做错了什么?如果没有使用像lambda这样的高级技术,请向Python新手解释。谢谢!

1 个答案:

答案 0 :(得分:1)

__init__()中你缺少自我。它应该是:

def __init__(self):

我不知道这是否是您唯一的问题,因为您没有发布所有代码(您提供的代码段将因许多其他原因而无法运行),但我假设这是您正在查找的内容对

修改

self._dialog_window.destroy() 

_dialog_window没有定义。

编辑2

command = self._ok_button_clicked())

应阅读:

command = self._ok_button_clicked)

列表没有销毁方法:

self._input_list.destroy()  # this will fail