dotmashrc
dotmashrc

Reputation: 318

Tkinter label and button not rendering

I have a tkinter window of dimensions 300x300 pixel and a button and a label that are supposed to appear but they are not. I am only starting with tkinter so not too sure what I am doing wrong from the tutorial I am following.

import tkinter as tk

class Window(tk.Frame):

    def __init__(self, container):
        super().__init__(container)

        self.label = tk.Label(self, text = "Hello, World!")
        self.label.grid(row = 0, column = 0)

        self.button = tk.Button(self, text = "PI")
        self.button["command"] = self.value_of_pi
        self.button.grid(row = 1, column = 1)


    def value_of_pi(self):
        tk.messagebox.showinfo(title = "Value of PI", message = "The value of PI is 3.14.")


class App(tk.Tk):
    
    def __init__(self, dimensions):
        super().__init__()

        self.title("App")
        self.geometry(dimensions)


if __name__ == "__main__":
    app = App("300x300")
    frame = Window(app)

    app.mainloop()

The window that appears

myoutput

Upvotes: 0

Views: 75

Answers (1)

Corralien
Corralien

Reputation: 120391

You forgot frame.pack()

...

if __name__ == "__main__":
    app = App("300x300")
    frame = Window(app)
    frame.pack()  # <- HERE
    app.mainloop()

enter image description here

Upvotes: 2

Related Questions