Skip to content Skip to sidebar Skip to footer

Python Tkinter Button Not Appearing?

I'm new to tkinter and I have this code in python: #import the tkinter module from tkinter import * import tkinter calc_window = tkinter.Tk() calc_window.title('Calculator Progra

Solution 1:

Getting a widget to appear requires two steps: you must create the widget, and you must add it to a layout. That means you need to use one of the geometry managers pack, place or grid to position it somewhere in its container.

For example, here is one way to get your code to work:

button_1 = tkinter.Button(text = '1', width = '30', height = '20')
button_1.pack(side="top")

The choice of grid or pack is up to you. If you're laying things out in rows and columns, grid makes sense because you can specify rows and columns when you call grid. If you are aligning things left-to-right or top-to-bottom, pack is a little simpler and designed for such a purpose.

Note: place is rarely used because it is designed for precise control, which means you must manually calculate x and y coordinates, and widget widths and heights. It is tedious, and usually results in widgets that don't respond well to changes in the main window (such as when a user resizes). You also end up with code that is somewhat inflexible.

An important thing to know is that you can use both pack and grid together in the same program, but you cannot use both on different widgets that have the same parent.


Solution 2:

from tkinter import *
import tkinter


calc_window = tkinter.Tk()
calc_window.title('Calculator Program')
frame = Frame(calc_window )
frame.pack()


button_1 = tkinter.Button(frame,text = '1', width = '30', height = '20')
button_1.pack(side=LEFT)


calc_window.mainloop()

try adding the button using pack(). i donno why u tried to assign button_1 = '1' in your code

a neat example:

from Tkinter import *

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(
            frame, text="QUIT", fg="red", command=frame.quit
            )
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print "hi there, everyone!"

root = Tk()

app = App(root)

root.mainloop()

Solution 3:

You're not packing the button_1. The code is:

from tkinter import *

root = Tk()
root.title('Calculator Program')

button_1 = Button(root, text='1', width='30', height='20')
button_1.pack()

root.mainloop()

It's simple! Hope this helps!


Solution 4:

from tkinter import *

calc_window = Tk()

calc_window.title('Calculator Program')

button_1 = Button(text = '1')

button_1.place(x=0,y=0,width = 30, height = 20)

calc_window.mainloop()

Post a Comment for "Python Tkinter Button Not Appearing?"