Python 按钮的像素宽度和高度?

Python 按钮的像素宽度和高度?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,在我的Tkinter程序中,我尝试了一种用Python扩展按钮大小的方法。当我尝试这些“宽度”和“高度”的东西时,我得到的只是一些看起来非常混乱的东西,可能只是指字体大小方面的宽度和高度。当我试图在每个数字的末尾加上“px”时,我得到了一个错误。如何调整按钮的大小(以像素为单位) 这是我目前的代码 from tkinter import * import tkinter as tk root = tk.Tk() root.geometry("960x600") button_qwer = B

在我的Tkinter程序中,我尝试了一种用Python扩展按钮大小的方法。当我尝试这些“宽度”和“高度”的东西时,我得到的只是一些看起来非常混乱的东西,可能只是指字体大小方面的宽度和高度。当我试图在每个数字的末尾加上“px”时,我得到了一个错误。如何调整按钮的大小(以像素为单位)

这是我目前的代码

from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry("960x600")

button_qwer = Button(root, text="asdfasdf", width="10", height="10")
button_asdf = Button(root, text="asdfasdf", width="20", height="20")
button_zxcv = Button(root, text="asdfasdf", width="30", height="30")

button_qwer.grid(row=0, column=0)
button_asdf.grid(row=0, column=1)
button_zxcv.grid(row=0, column=2)

root.mainloop()

如果按钮有图像,则宽度和高度以像素为单位。如果没有图像,则宽度和高度为基于按钮所用字体的字符“0”(零)大小的字符数。如果同时包含图像和文本,则值以像素为单位

使用不可见图像 一种解决方案是为其提供一个不可见的图像,以便将属性视为像素。由于边框和突出显示环等额外装饰,按钮的大小仍然不会那么精确。如果需要精确的大小,则需要将这些选项也设置为零,或者调整宽度以考虑边框宽度

例如:

from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry("960x600")

null_image = tk.PhotoImage(width=0, height=0)

button_qwer = Button(root, text="asdfasdf", width="10", height="10",
                     image=null_image, compound="center", borderwidth=0,
                     highlightthickness=0, padx=0, pady=0)
button_asdf = Button(root, text="asdfasdf", width="20", height="20",
                     image=null_image, compound="center", borderwidth=0,
                     highlightthickness=0, padx=0, pady=0)
button_zxcv = Button(root, text="asdfasdf", width="30", height="30",
                     image=null_image, compound="center", borderwidth=0,
                     highlightthickness=0, padx=0, pady=0)

button_qwer.grid(row=0, column=0)
button_asdf.grid(row=0, column=1)
button_zxcv.grid(row=0, column=2)

root.mainloop()
from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry("960x600")

f1 = tk.Frame(root, width=10, height=10)
f2 = tk.Frame(root, width=20, height=20)
f3 = tk.Frame(root, width=30, height=30)

f1.grid(row=0, column=0)
f2.grid(row=0, column=1)
f3.grid(row=0, column=2)

button_qwer = Button(f1, text="asdfasdf")
button_asdf = Button(f2, text="asdfasdf")
button_zxcv = Button(f3, text="asdfasdf")

button_qwer.place(x=0, y=0, relwidth=1, relheight=1)
button_asdf.place(x=0, y=0, relwidth=1, relheight=1)
button_zxcv.place(x=0, y=0, relwidth=1, relheight=1)

root.mainloop()
使用框架 另一种解决方案是创建一个具有特定大小的框架,然后使用
place
将按钮放入框架中,使其填充框架

例如:

from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry("960x600")

null_image = tk.PhotoImage(width=0, height=0)

button_qwer = Button(root, text="asdfasdf", width="10", height="10",
                     image=null_image, compound="center", borderwidth=0,
                     highlightthickness=0, padx=0, pady=0)
button_asdf = Button(root, text="asdfasdf", width="20", height="20",
                     image=null_image, compound="center", borderwidth=0,
                     highlightthickness=0, padx=0, pady=0)
button_zxcv = Button(root, text="asdfasdf", width="30", height="30",
                     image=null_image, compound="center", borderwidth=0,
                     highlightthickness=0, padx=0, pady=0)

button_qwer.grid(row=0, column=0)
button_asdf.grid(row=0, column=1)
button_zxcv.grid(row=0, column=2)

root.mainloop()
from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry("960x600")

f1 = tk.Frame(root, width=10, height=10)
f2 = tk.Frame(root, width=20, height=20)
f3 = tk.Frame(root, width=30, height=30)

f1.grid(row=0, column=0)
f2.grid(row=0, column=1)
f3.grid(row=0, column=2)

button_qwer = Button(f1, text="asdfasdf")
button_asdf = Button(f2, text="asdfasdf")
button_zxcv = Button(f3, text="asdfasdf")

button_qwer.place(x=0, y=0, relwidth=1, relheight=1)
button_asdf.place(x=0, y=0, relwidth=1, relheight=1)
button_zxcv.place(x=0, y=0, relwidth=1, relheight=1)

root.mainloop()

每种方法都有什么具体的优点或缺点吗?@RonZhang:不完全是,除了使用框架意味着你有更多的小部件之外。我不认为这有什么大不了的。当某些情况下无法编写某些附加代码时,可能会出现一些潜在的缺点/优点。目前,我假设除了有更多的小部件之外,所有的代码片段几乎都是相同的,但是我不太确定除了小部件的数量之外,它是否真的没有什么问题。您想向我验证一下,在使用不可见图像和帧之间添加某些其他代码时,您是否发现了任何潜在问题?对于不可见图像示例,我可能发现的一个潜在缺点是,我可能无法同时放置不可见图像和不可见图像在同一个按钮上,不是吗?我还没有测试过,但是我很想知道每种方法的优缺点。@RonZhang:如果你需要一个不可见的图像,你就不需要一个不可见的图像。你只需要一张照片。