如何设置按钮的大小(以像素为单位) - python

时间:2018-04-14 10:14:15

标签: python python-3.x tkinter

我使用的是Python 3,我想设置按钮的大小(以像素为单位)。

我想让它宽度= 100像素,高度= 30像素,但它没有用。

它比我预期的要大得多。

这是我的代码:

from tkinter import *

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

    btn_easy = Button(root, text = 'Easy', width = 100, height = 50)
    btn_easy.place(x = 100, y = 450)

    root.mainloop()

background()

我该怎么做?

1 个答案:

答案 0 :(得分:2)

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

  

您还可以使用height和width选项显式设置   尺寸。 如果您在按钮中显示文字,这些选项会定义尺寸   按文字为单位的按钮。如果您显示位图或图像,   它们以像素(或其他屏幕单位)定义大小。 你可以   指定文本按钮的大小(以像素为单位),但这需要   有些神奇。这是一种方法(还有其他方式):

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()

奖金:许多按钮(只是为了得到这个想法)

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()