Jake Nelson
Jake Nelson

Reputation: 2063

Why does my tkinter app have an extraneous 'tk' window?

I'm learning Python & Tkinter. I'm pretty new to them both.

I'm trying to make a Python app for the Raspberry Pi that mimics the PipBoy 3000 Mark IV from Fallout 4.

At the moment I'm just working on the intro animation part of the program which simply animates some text one character at a time.

I'm using Python 3.2 by the way, on both Linux (ChromeOS via Crouton) and Windows. I've already done several tkinter tutorials but I must be missing something here. If folks could point me in the right direction I'd appreciate it.

main.py:

from tkinter import *
from tkinter import ttk

from introAnimation import *

# FUNCTIONS


def setup_root():
    global root
    root = Tk()
    root.title("PipBoy 3000 Mark IV")
    root.geometry("800x480")
    root.bind("<Escape>", lambda e: e.widget.quit())


def setup_intro():
    global introframe
    introframe = ttk.Frame(root, style='pip.TFrame', padding="3 3 12 12")
    print('introframe created.')
    introframe.grid(column=0, row=0, sticky=(N, W, E, S))
    introframe.columnconfigure(0, weight=1)
    introframe.rowconfigure(0, weight=1)
    ttk.Label(introframe, width=200, textvariable=introHeading).grid(column=4, row=1, sticky=(W, E))
    global dynamic_label
    dynamic_label = ttk.Label(introframe, style='pip.TLabel', textvariable=temp_label).grid(column=1, row=2)
    print('dynamnic_label created')

def print_label_slowly(message):
    array = list(message)
    print('print_label_slowly was called')
    for char in array:
        print("in loop, char: %s" % char)
        text = temp_label.get()
        text += char
        introframe.after(600)
        temp_label.set(text)
        introframe.update_idletasks()


# MAIN
if __name__ == "__main__":
    # set up frame style
    pip_frame = ttk.Style()
    pip_frame.configure('pip.TLabel', background='black', foreground='green')
    pip_frame.configure('pip.TFrame', background='black', foreground='green')
    # set up root geometry
    setup_root()

    # set up intro
    temp_label = StringVar()
    setup_intro()
    introframe.focus()
    for postMessage in introAnimArray:
        print_label_slowly(postMessage)
    root.update_idletasks()
    # print_label_slowly('test')
    root.mainloop()

Some of the strings are being taken from IntroAnimation.py IntroAnimation.py:

introAnimArray = [
    "LOADER V1.1",
    "EXEC VERSION 41.10",
    "64K RAM SYSTEM",
    "38911 BYTES FREE",
    "NO HOLOTAPE FOUND",
    "LOAD ROM(1). DEITRIX 303"
]

introHeading = "*************** PIP-OS(R) V7.1.0.8 ***************"

initialiseString = 'INITIATING'

The issues:

Edit: Removed extra questions to focus on the key issue. Result of issues

Upvotes: 0

Views: 102

Answers (1)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19219

Too much code, too many questions. This should be three separate questions with a small bit of code for each. Read and act on this SO help page.

Lets make this question about the extraneous little window labelled 'tk'. You are assuming that it has something to do with introframe, but you don't know that. If you had developed your code incrementally, testing as you go, you would have noticed which addition made it appear. Given what you have, delete until the problem disappears.

Here is what I did. I commented out everything between setup_root and root.mainloop. Still got two windows. Removing more, it turns out that style() creates a blank tk window if there is not one already. If you had run you code with that one line in the main clause, you would have seen the problem immediately. Create root first and the problem disappears.

I used 3.5. Use something newer than 3.2 if you can with RPy.

Upvotes: 1

Related Questions