-1

This is the error I'm getting when button to open new window is pressed.

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
    return self.func(*args)
  File "/Users/Jaguar/PycharmProjects/learnpython/chemcalc.py", line 78, in <lambda>
    Button(master, text='Sub', width=3, command=lambda: self.sub_win()).grid(row=2, column=6)
  File "/Users/Jaguar/PycharmProjects/learnpython/chemcalc.py", line 57, in sub_win
    top = Toplevel(self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2336, in __init__
    BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2286, in __init__
    BaseWidget._setup(self, master, cnf)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2256, in _setup
    self.tk = master.tk
AttributeError: 'calc' object has no attribute 'tk'

My code:

from tkinter import *


class calc:

    def sub_win(self):
        top = Toplevel(self)


    def __init__(self, master):
        master.title('Title')
        master.geometry()
        self.e = Entry(master, justify = RIGHT)
        self.e.grid(row=0, column=0, columnspan=8, pady=3)
        self.answerlist = []

root = Tk()
obj=calc(root)
root.mainloop()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
jaguar419
  • 13
  • 4
  • Please take the time to read this: stackoverflow.com/help/how-to-ask . It is easier for use to help when we are able to understand the problem. – Christian Will Nov 19 '17 at 05:50
  • always put full error message (Traceback). There are other useful info.Can you create `Toplevel` directly - without class? – furas Nov 19 '17 at 06:03
  • BTW: you execute `calc()` but you don't have `__init__(self, parent)` and `Toplevel` uses `self` (which is not tkinter widget) but it need `root`. – furas Nov 19 '17 at 06:05
  • I didn't post the full code, i have __init__(self, master) – jaguar419 Nov 19 '17 at 06:10
  • Your code works. The error message does not correspond to the code you share. – Billal Begueradj Nov 19 '17 at 06:19
  • The code should work, which is why I'm at such a loss. This error comes up button is pressed. I'm using a mac. – jaguar419 Nov 19 '17 at 06:36

1 Answers1

1

Your problem is self in Toplevel(self)

Most widgets need parent as first argument which have to be tkinter widget.

In your code self is not tkinter widget.
You have to use master as you did in Entry(master)

top = Toplevel(master)

but you will have to keep master as self.master.

Toplevel can also run without parent

top = Toplevel()

and it will use Tk() as parent.

furas
  • 134,197
  • 12
  • 106
  • 148
  • The traceback suggests the error is inherent to a button press, IMHO it would be better to encourage the OP to share the code that corresponds to the actual error message. – Billal Begueradj Nov 19 '17 at 12:05
  • @BillalBEGUERADJ error message in question shows problem in `top = Toplevel(self)`. And there is corresponds code in question. – furas Nov 19 '17 at 12:18