a_ko
a_ko

Reputation: 159

tkinter window empty , not displaying text

I'm running a simple tutorial on tkinter but for some strange reason my window comes up empty and not displaying the 'text=' whatsoever. I browsed all the related Qs (there are a few here) but none gave a solution.

The script executes without an error message but the window is not meant to be empty. Any advice would be appreciated as I feel like I explored all dead ends..

Here's the code:

import tkinter as tk
LARGE_FONT=("Verdana", 12)
class SeaofBTCapp (tk.Tk):
    def _init_(self,*args,**kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        frame = StartPage(container, self)
        self.frames[StartPage] = frame
        frame.grid(row=0, column = 0, sticky="nsew")
        self.show_frame(StartPage)


    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):
    def _init_(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10, padx=10)



app= SeaofBTCapp()
app.mainloop()

Python version: 3.5.2 |Anaconda 4.1.1 (64-bit)

Some other Qs on the subject discussing not using .frame and .pack together , I have tried deleting either .frame line or the .pack line but same result. Script runs w/o error but the window is empty.

Thanks!

Upvotes: 0

Views: 427

Answers (2)

a_ko
a_ko

Reputation: 159

As per Steven's answer:

You need two underscores for __init__ – Steven Summers

I was simply blind from staring at the lines looking for where it goes wrong.. Thanks.

Upvotes: 0

Jake
Jake

Reputation: 912

The reason this code didn't work is because you did not display init correctly. You used _init_, which should have 2 underscores per each, e.g. __init__. Here's what you should have done:

import tkinter as tk
LARGE_FONT=("Verdana", 12)
class SeaofBTCapp (tk.Tk):
    def _init_(self,*args,**kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        frame = StartPage(container, self)
        self.frames[StartPage] = frame
        frame.grid(row=0, column = 0, sticky="nsew")
        self.show_frame(StartPage)


    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):
    def _init_(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10, padx=10)



app = SeaofBTCapp()
app.mainloop()

Also, you were using python 3.5 and this code works better in python 2.

Hope I could help :)

Upvotes: 1

Related Questions