CalmBob CalmBob - 5 months ago 16
Python Question

Python tkinter: buttons are not displaying

I'm new to tkinter and I'm trying to display two buttons on the window. Currently the buttons are not displaying on the window. I would be very grateful if someone could point out my errors. Thank you.

class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()

def creat_widgets(self):
self.button1 = Button(self, text="I do nothing")
self.button1.grid()

self.button2 = Button(self)
self.button2.grid()

self.button2.config(text="Me too!")

Answer

There is a problem in your __init__bmethod, and from what I see you do not launch the application anywhere. Does this work for you?

from Tkinter import *

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.button1 = Button(self, text="I do nothing")
        self.button1.grid()

        self.button2 = Button(self)
        self.button2.grid()

        self.button2.config(text="Me too!")

if __name__ == '__main__':
    root = Tk()
    Application(root).mainloop()