【问题标题】:How can I set the size of a button in pixels - python [duplicate]如何以像素为单位设置按钮的大小 - python [重复]
【发布时间】:2018-04-14 10:14:14
【问题描述】:

我使用的是 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()

我该怎么做?

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

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

    您还可以使用高度和宽度选项来显式设置 尺寸。 如果您在按钮中显示文本,这些选项将定义大小 按钮的文本单位​​strong>。如果您改为显示位图或图像, 它们以像素(或其他屏幕单位)为单位定义大小。 你可以 甚至为文本按钮指定像素大小,但这需要 一些魔法。这是一种方法(还有其他方法):

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 2020-02-16
    • 2011-08-25
    • 2015-07-01
    • 1970-01-01
    • 2018-07-04
    • 2020-12-14
    • 2016-09-13
    相关资源
    最近更新 更多