【发布时间】:2018-10-23 13:57:23
【问题描述】:
所以到目前为止我有这个工作应用程序:
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 像素宽的按钮(使用 screenruler 测量)。
这个 GUI 只能在固定的硬件上运行,所以如果可能的话,我想继续使用 place 管理器在窗口上放置东西。
所以,总结一下:为什么我的按钮比我设置的宽度宽 25 像素?
在 Ubuntu 18.04 上使用 Python 3,以防万一。
谢谢
【问题讨论】:
-
您是否尝试过将每个按钮放在自己的框架中?我在看这篇文章,Displaying square Tkinter.Button's?,它说一旦你把按钮放在一个框架中,这样它就会把按钮当作图像并使用像素而不是文本大小
-
嗨,@B.Cratty。我知道有解决方法,感谢您提供的链接,这也解决了复合按钮的“情况”。我的问题更多的是“为什么会发生这种情况”,而不是“我该如何解决这个问题”(我相信这也是你一开始没有回答的原因?)。不过,谢谢你的链接。
-
不客气,很抱歉我帮不上什么忙。我能找到的唯一能帮助回答您的帖子的是我提供的链接。
标签: python-3.x button tkinter