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

时间:2019-03-02 17:59:18

标签: python tkinter

我正在尝试为Tic Tac Toe游戏创建一个窗口。在主根窗口中(尺寸->高度= 700,宽度= 600),我创建了两个框架。

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

2:底部框架称为 StatArea ,其尺寸为-> Height = 100和width = 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的顶部框架。但是,正如您所看到的,按钮正变得越来越大。为什么会这样呢?

1 个答案:

答案 0 :(得分:1)

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

  

height =
  按钮的高度。如果按钮显示文本,则尺寸以文本单位给出。如果按钮显示图像,则尺寸以像素(或屏幕单位)给出。如果省略大小,则根据按钮内容进行计算。 (高度/高度)

     

width =
  按钮的宽度。如果按钮显示文本,则尺寸以文本单位给出。如果按钮显示图像,则尺寸以像素(或屏幕单位)给出。如果省略大小或为零,则根据按钮内容进行计算。 (宽度/宽度)

您的按钮包含text = "",因此尺寸以文本单位为单位,大于像素。

通常,您无需显式设置每个帧的大小,尤其是要使它们都具有相同的大小时。例如here