如何以像素为单位设置按钮的大小 - python [英] How can I set the size of a button in pixels - python

查看:56
本文介绍了如何以像素为单位设置按钮的大小 - python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Python 3, and I want to set the size of a button in pixels.

我想让它宽度 = 100 像素,高度 = 30 像素,但没有成功.

I wanted to make it width = 100 pixels, height = 30 pixels, but it didn't work.

它比我预期的要大得多.

It was much bigger than I expected.

这是我的代码:

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

我该怎么做?

推荐答案

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

这篇关于如何以像素为单位设置按钮的大小 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆