tkinter按键布局
时间: 2023-08-31 11:10:41 浏览: 67
Python tkinter布局与按钮间距设置方式
在Tkinter中,可以使用grid()、pack()或place()等函数来布局按键。
使用grid()函数布局按键:
```python
import tkinter as tk
root = tk.Tk()
btn1 = tk.Button(root, text="Button 1")
btn1.grid(row=0, column=0)
btn2 = tk.Button(root, text="Button 2")
btn2.grid(row=0, column=1)
btn3 = tk.Button(root, text="Button 3")
btn3.grid(row=1, column=0)
btn4 = tk.Button(root, text="Button 4")
btn4.grid(row=1, column=1)
root.mainloop()
```
使用pack()函数布局按键:
```python
import tkinter as tk
root = tk.Tk()
btn1 = tk.Button(root, text="Button 1")
btn1.pack(side="left")
btn2 = tk.Button(root, text="Button 2")
btn2.pack(side="left")
btn3 = tk.Button(root, text="Button 3")
btn3.pack(side="right")
btn4 = tk.Button(root, text="Button 4")
btn4.pack(side="right")
root.mainloop()
```
使用place()函数布局按键:
```python
import tkinter as tk
root = tk.Tk()
btn1 = tk.Button(root, text="Button 1")
btn1.place(x=20, y=20)
btn2 = tk.Button(root, text="Button 2")
btn2.place(x=80, y=20)
btn3 = tk.Button(root, text="Button 3")
btn3.place(x=20, y=60)
btn4 = tk.Button(root, text="Button 4")
btn4.place(x=80, y=60)
root.mainloop()
```
阅读全文