I am trying to create Buttons in Python with classes, but when running it the buttons do not appear. Following is my code
#Button_2
#Using Classes
from Tkinter import *
class Application(Frame):
"""A GUI application with three button"""
def _init_(self, master):
""" Initialise the Frame. """
Frame._init_(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
#"""Create three buttons"""
#Create first buttom
self.btn1 = Button(self, text = "I do nothing")
self.btn1.grid()
#Create second button
self.btn2 = Button(self)
self.btn2.grid()
self.btn2.configure(text = "T do nothing as well")
#Create third button
self.btn3 = Button(self)
self.btn3.grid()
self.btn3.configure(text = "I do nothing as well as well")
root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
root.mainloop()
Any help would be appreciated
OK, first problem is that you have declared your following code:
inside the class itself. It should be outside, so this an indentation problem (maybe stackoverflow problem with indents?).
secondly I simplified the code to get it to run
This is a starting point and definitely works as proven below:
You can probablty muck around with the grid() instead of pack and call the method from the def init constructor. Hope it helps.
This calling method also works:
My final try also works:
followed by:
The final code: