Q: AttributeError: '_tkinter.tkapp' object has no attribute 'x' Here is my code:
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter.messagebox import *


class Application(tk.Tk):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.title('test')
        self.geometry('300x350')
        self.setup_ui()
        self.ctrl()
        self.tmp = True

    def ctrl(self):
        if self.tmp == True:
            showwarning(title='warning',message='warning')

    def setup_ui(self):
        menubar = tk.Menu(self)
        menuFile = tk.Menu(menubar)
        menubar.add_cascade(label='File', menu=menuFile)
        menuFile.add_command(label='Exit', command=self.ctrl())
        self.configure(menu=menubar)


if __name__ == '__main__':
    app = Application()
    app.mainloop()


When i run this code:
i got this:
Traceback (most recent call last):
  File "E:/calc/test.py", line 29, in <module>
    app = Application()
  File "E:/calc/test.py", line 12, in __init__
    self.setup_ui()
  File "E:/calc/test.py", line 24, in setup_ui
    menuFile.add_command(label='Exit', command=self.ctrl())
  File "E:/calc/test.py", line 17, in ctrl
    if self.tmp == True:
  File "D:\py3.7\lib\tkinter\__init__.py", line 2101, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'tmp'

i think the problem is caused self.tmp = True and the tkinter think it's a order, but i jusy want to let it as a variable.
i don't know why and how fix the problem.
Thank you.

A: command=self.ctrl() immediately calls self.ctrl() and assigns the result to command.
You also have the problem that in __init__ you call self.ctrl before assigning a default value to self.tmp
