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 :).

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Cxnnxr09
  • 11
  • 4

1 Answers1

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.

figbeam
  • 7,001
  • 2
  • 12
  • 18