首页 > 解决方案 > 关于网格的 Python tkinter 几何管理

问题描述

为了简单起见,我问一个简单的问题,

我想做 4 个按钮,每个(全填充)都停留在框架容器或 tkinter 窗口的一个角落:

button_1 = tkinter.Button(window, text="Button 1")
button_2 = tkinter.Button(window, text="Button 2")
button_3 = tkinter.Button(window, text="Button 3")
button_4 = tkinter.Button(window, text="Button 4")
button_1.grid(row=0, column=0)
button_2.grid(row=0, column=1)
button_3.grid(row=1, column=0)
button_4.grid(row=1, column=1)

但是,它们都是仅在窗口左上角保持在一起的小按钮,它们并没有像预期的那样填满整个窗口。

标签: pythontkintergeometrygrid

解决方案


btn.place如果您不想使用网格系统,也可以使用基于坐标系放置按钮。请检查代码段

按钮角

from tkinter import *
import tkinter as tk
window = Tk()                 
window.geometry('160x130')             
button1 = Button(window, text="Button 1")
button1.place(x=0,y=0)
button2 = Button(window, text="Button 2")
button2.place(x=100,y=0)
button3 = Button(window, text="Button 3")
button3.place(x=0,y=100)
button4 = Button(window, text="Button 4")
button4.place(x=100,y=100)
window.mainloop() 

推荐阅读