0

I've been doing a Gui multiple windows quest but Tkinter doesn't seem to have Tk. My full error is

Traceback (most recent call last):
  File "/Users/connorsmacbook/PycharmProjects/2.8/2.8 Internal/TextTypers 2.2.py", line 6, in <module>
    class TextTypers(tk.TK):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2101, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'TK'

My code is

from tkinter import *
tk=Tk()

# Classes
class TextTypers(tk.TK):

    def __init__(self, *args, **kwargs): # Runs when our class is called and allows almost anything to be passed

        tk.Tk.__init__(self, *args, **kwargs)  # Initialise Tk
        window = tk.Frame(self)  # Creates the container the windows/frames will populate
        window.pack()

        self.frames = {}  # Creates a dictionary for the frames

        frame = MenuScreen(window, self)
        self.frames[MenuScreen] = frame
        frame.grid(row=0, column=0, sticky="nswe")
        self.show_frame(MenuScreen)  # Shows the menu screen as this is initialising

    def show_frame(self, cont):

        frame = self.frames[cont]  # Grabs value of self.frames and puts in in frame
        frame.tkraise()  # Raises frame to the front

class MenuScreen(tk.frame): # Inherits everything from the frame

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) # Inherits from main class
        label = tk.Label(self, text="Menu")
        label.pack()

run = TextTypers()
run.mainloop()

If any wizards could help I would be grateful :).

0

1 Answer 1

2

The line

tk=Tk()

creates an instance of Tk() with the name tk.

When you create the class

class TextTypers(tk.TK):

you are trying to inherit an attribute called TK from the instance tk.

In general, I would not use the name tk for the root window as tk is usually used as an alias for the tkinter module.

I think what you are after is something like this:

import tkinter as tk

# Classes
class TextTypers(tk.Tk):

    def __init__(self, *args, **kwargs): # Runs when our class is called and allows almost anything to be passed

        tk.Tk.__init__(self, *args, **kwargs)  # Initialise Tk
        window = tk.Frame(self)  # Creates the container the windows/frames will populate
        window.pack()

        self.frames = {}  # Creates a dictionary for the frames

        frame = MenuScreen(window, self)
        self.frames[MenuScreen] = frame
        frame.grid(row=0, column=0, sticky="nswe")
        self.show_frame(MenuScreen)  # Shows the menu screen as this is initialising

    def show_frame(self, cont):

        frame = self.frames[cont]  # Grabs value of self.frames and puts in in frame
        frame.tkraise()  # Raises frame to the front

class MenuScreen(tk.Frame): # Inherits everything from the frame

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) # Inherits from main class
        label = tk.Label(self, text="Menu")
        label.pack()

run = TextTypers()
run.mainloop()

Have a look at Best way to structure a tkinter application were you can find some suggestions and discussion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.