为什么我在Python Tkinter中的按钮和框架没有在正确的几何坐标中排列,尽管给出了正确的尺寸

2024-09-29 20:21:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为我的井字游戏创造一个窗口。在主根窗口(dimensions->;height=700,width=600)中,我创建了两个框架。你知道吗

1:顶部框架称为ActionArea,其尺寸为->;Height=600,width=600

2:底部框架称为StatArea,其尺寸为->;高度=100,宽度=600

在顶部框架中,我放置了9个按钮,每个按钮的大小相等,为200*200

这是我的期望: My expectations on how the Window would look

这就是我运行代码的现实: this is the output I am getting on running my code

这是代码的相关部分:

root=tk.Tk()
root.title("TIC TAC TOE")
root.geometry("600x700")


#creating two frames
ActionArea=tk.Frame(root,height=600,width=600,bg="AliceBlue")
StatArea=tk.Frame(root,height=100,width=600,bg="RoyalBlue")

#placing the frames onto root window
ActionArea.grid(row=0,column=0)
StatArea.grid(row=1,column=0)

#creating the buttons
b1=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b2=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b3=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b4=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b5=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b6=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b7=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b8=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")
b9=tk.Button(ActionArea,text="", height=200,width=200,bg="AliceBlue")

#packing the buttons
b1.grid(row=0,column=0)
b2.grid(row=0,column=1)
b3.grid(row=0,column=2)
b4.grid(row=1,column=0)
b5.grid(row=1,column=1)
b6.grid(row=1,column=2)
b7.grid(row=2,column=0)
b8.grid(row=2,column=1)
b9.grid(row=2,column=2)

你知道吗太多了怀疑。 我把每个纽扣的尺寸精确到200*200,以适合我的顶部框架,这个框架的尺寸是600*600。然而,正如你所看到的,按钮变得大得离谱。为什么会这样?你知道吗


Tags: textgt框架尺寸columnbuttonrootwidth
1条回答
网友
1楼 · 发布于 2024-09-29 20:21:14

这正是因为您用height=200, width=200调整了按钮的大小。tkinter.Button高度和宽度并不总是以像素为单位。从documentation

height=
The height of the button. If the button displays text, the size is given in text units. If the button displays an image, the size is given in pixels (or screen units). If the size is omitted, it is calculated based on the button contents. (height/Height)

width=
The width of the button. If the button displays text, the size is given in text units. If the button displays an image, the size is given in pixels (or screen units). If the size is omitted, or zero, it is calculated based on the button contents. (width/Width)

您的按钮包含text = "",因此大小以文本为单位,大于像素。你知道吗

通常不需要显式设置每个帧的大小,特别是如果希望使它们的大小相同。看看here的例子。你知道吗

相关问题 更多 >

    热门问题