Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading