Skip to content Skip to sidebar Skip to footer

How Can I Set The Size Of A Button In Pixels - Python

I'm using Python 3, and I want to set the size of a button in pixels. I wanted to make it width = 100 pixels, height = 30 pixels, but it didn't work. It was much bigger than I expe

Solution 1:

http://effbot.org/tkinterbook/button.htm

You can also use the height and width options to explicitly set the size. If you display text in the button, these options define the size of the button in text units. If you display bitmaps or images instead, they define the size in pixels (or other screen units). You can specify the size in pixels even for text buttons, but that requires some magic. Here’s one way to do it (there are others):

f = Frame(master, height=32, width=32)
f.pack_propagate(0) # don't shrink
f.pack()

b = Button(f, text="Sure!")
b.pack(fill=BOTH, expand=1)

from tkinter import *

def background():
    root = Tk()
    root.geometry('1160x640')

    f = Frame(root, height=50, width=50)
    f.pack_propagate(0) # don't shrink
    f.place(x = 100, y = 450)

    btn_easy = Button(f, text = 'Easy')
    btn_easy.pack(fill=BOTH, expand=1)

    root.mainloop()

background()

Bonus: many buttons (just to get the idea)

from tkinter import *

def sizedButton(root, x,y):

    f = Frame(root, height=50, width=50)
    f.pack_propagate(0) # don't shrink
    f.place(x = x, y = y)

    btn_easy = Button(f, text = 'Easy')
    btn_easy.pack(fill=BOTH, expand=1)


def background():
    root = Tk()
    root.geometry('1160x640')

    for x in range(50,350,100):
        for y in range(50,350,100):
            sizedButton(root, x,y)


    root.mainloop()

background()

Post a Comment for "How Can I Set The Size Of A Button In Pixels - Python"