tkinter设置程序界面在屏幕正中心
时间: 2023-10-19 21:48:31 浏览: 91
要将Tkinter程序界面设置在屏幕的正中心,可以使用以下步骤:
1. 导入`tkinter`模块:
```python
import tkinter as tk
```
2. 创建主窗口,并设置窗口的大小和标题:
```python
root = tk.Tk()
root.geometry("300x200") # 设置窗口大小
root.title("My Application") # 设置窗口标题
```
3. 使用`winfo_screenwidth()`和`winfo_screenheight()`方法获取屏幕的宽度和高度:
```python
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
```
4. 计算窗口的左上角坐标,使其位于屏幕正中心:
```python
window_width = 300 # 窗口宽度
window_height = 200 # 窗口高度
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}") # 设置窗口位置
```
5. 进入主事件循环:
```python
root.mainloop()
```
完整的示例代码如下:
```python
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
root.title("My Application")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 300
window_height = 200
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
root.mainloop()
```
这样,Tkinter程序界面就会显示在屏幕的正中心位置。
阅读全文
相关推荐


















