所以到目前为止,我已经有了这个可以运行的应用程序:
import tkinter as tk
class AppGui(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# Set window size
self.geometry("400x600")
# Create main container frame
self.cframe = tk.Frame(self,
bg = "yellow",
width = 400,
height = 600
)
self.cframe.place(x=0,y=0)
# Draw the first page and move on
self.Draw()
def Draw(self):
# Create top bar and label
topbar = tk.Frame(self.cframe,bg="green",width=400,height=60)
topbar.place(x=0,y=0)
l = tk.Label(topbar,
text = "TOP Title",
bg = "red"
)
l.place(x=200,y=30,anchor="center")
# Create center container
centerpanel = tk.Frame(self.cframe,bg="orange",width=400,height=480)
centerpanel.place(x=0,y=60)
# Create a one-pixel image
pixel = tk.PhotoImage(width=1, height=1)
# Create program select button group
b_left = tk.Button(centerpanel,
text = "Left",
image = pixel, # This allows me to specify width in pixels.
width = 75,
height = 50,
compound = "c"
)
b_left.place(x=25,y=30)
b_center = tk.Button(centerpanel,
text = "Center",
image = pixel, # This allows me to specify width in pixels.
width = 75,
height = 50,
compound = "c"
)
b_center.place(x=150,y=30)
b_right = tk.Button(centerpanel,
text = "Right",
image = pixel, # This allows me to specify width in pixels.
width = 75,
height = 50,
compound = "c"
)
b_right.place(x=275,y=30)
if __name__ == "__main__":
gui = AppGui()
gui.mainloop()
基本上,我想要获得的是带有标签的顶部栏,带有三个并排按钮(带有一些填充)的中央面板和底部栏,我尚未实现。
我使每个组件都具有不同的颜色,有了它,我就可以继续前进。
Howeber,要使该按钮正常工作,我必须将所有按钮的 width 属性设置为75像素,以便使它们显示为100像素宽的按钮(使用屏幕尺测量)。
此GUI仅将在固定硬件上运行,因此,如果可能的话,我想继续使用 place 管理器在窗口中放置内容。
因此,总而言之:为什么我的按钮比我设置的宽度宽25像素?
在有意义的情况下,在Ubuntu 18.04上使用Python 3。
谢谢
答案 0 :(得分:0)
按钮的最终宽度不仅考虑指定的宽度,而且还考虑了按钮上可能设置的任何填充。由于您依赖填充的默认值,因此它可能非零。添加padx=0
来查看它如何影响您的按钮(例如:b_right = tk.Button(..., width=100, padx=0)
。
另一个选择是使用place
来强制设置小部件的大小。只需在呼叫位置时添加一个width
和height
(例如:b_right.place(x=275,y=30, width=100)
,它将覆盖按钮的设置。