Python Forum

Full Version: Buttons not appearing, first time button making
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have been watching python tutorials and finally got to GUI and have been trying to figure out buttons, I typed in the code as instructed however when the window opens there is still no quit button. Been looking at this for a while now and can't seem to figure out whats wrong. Am I perhaps missing a tkinter file? Here is the code:

from tkinter import *

class Window (Frame) :

    def __init__(self, master = None) :
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

def init_window(self) :
    self.master.title("GUI")
    self.pack(fill=BOTH, expand=1)
    quitButton = Button(self, text="Quit")
    quitButton.place(x=0, y=0)

root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
the indentation is wrong

try

from tkinter import *
 
class Window (Frame) :
 
    def __init__(self, master = None) :
        Frame.__init__(self, master)
        self.master = master
        self.init_window()
 
    def init_window(self) :
        self.master.title("GUI")
        self.pack(fill=BOTH, expand=1)
        quitButton = Button(self, text="Quit")
        quitButton.place(x=0, y=0)
 
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
Your code, cleaned up, so it actually works (find another tutorial that doesn't include the unnecessary crap, and explains things, like http://effbot.org/tkinterbook/button.htm or one of these https://wiki.python.org/moin/TkInter ). Note that the button doesn't do anything because there is no "command=" provided.
from tkinter import *

class Window() :
     def __init__(self, master = None) :
        self.master = master
        self.init_window()

     def init_window(self):
        self.master.title("GUI")
        quitButton = Button(self.master, text="Quit", bg="red")
        quitButton.grid()

root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop() 
Thanks for the assistance! I didn't even notice the indentation lol, however it the quit button still isn't showing up with the window.

The cleaned up version also still didn't seem to bring the quit button up but I appreciate the assistance very much and am looking into those tutorials!
I changed the background of the button to red to make it obvious to you. If you still don't see the button, post the new code that you are using.
Thanks! That worked!