Python tkinter如何把窗口在屏幕中央居中

该代码示例展示了如何利用Tkinter库使创建的窗口在启动时自动居中于用户的屏幕。通过获取屏幕尺寸并计算中心点坐标,然后调整窗口的位置实现此功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python tkinter如何把窗口在屏幕中央居中

# -*- coding: utf-8 -*-
import tkinter as tk

# 创建 Tkinter 窗口
root = tk.Tk()
# 设置窗口大小
root.geometry("400x300")

# 获取屏幕宽度和高度
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()

# 计算窗口显示时的左上角坐标
centerX = int((screenWidth - root.winfo_reqwidth()) / 2)
centerY = int((screenHeight - root.winfo_reqheight()) / 2)

# 设置窗口显示时的左上角坐标
root.geometry("+{}+{}".format(centerX , centerY ))

# main function
if __name__ == '__main__':
	# 窗口主循环
	root.mainloop()
### 回答1: 你好!要在Python中创建窗口界面,可以使用Tkinter模块。 以下是一个简单的例子,展示如何创建一个简单的窗口: ```python import tkinter as tk root = tk.Tk() # 创建一个窗口 # 设置窗口标题 root.title("我的窗口") # 设置窗口大小 root.geometry("300x200") # 显示窗口 root.mainloop() ``` 你可以运行这段代码,看看它创建了一个多大为300x200的名为“我的窗口”的窗口。 在这个窗口中,你可以添加各种小部件(widgets),如标签、按钮、文本框等。以下是一个例子,展示如何添加一个标签: ```python import tkinter as tk root = tk.Tk() # 创建一个标签 label = tk.Label(root, text="欢迎来到我的窗口!") # 把标签添加到窗口中 label.pack() root.mainloop() ``` 你可以运行这段代码,看看它在窗口中添加了一个标签。 希望这能帮到你!如果你有任何其他问题,请随时问我。 ### 回答2: 使用Python中的Tkinter库创建窗口时,有时需要将其居中显示,而不是在屏幕左上角显示。这可以通过简单的代码完成,具体如下: 首先,需要使用Tkinter库创建一个窗口对象并设置其宽度和高度。可以使用root = tk.Tk()来创建窗口对象。接下来,需要设置窗口宽度和高度。例如,可以使用root.geometry('500x500') 将窗口设置为500像素宽和500像素高。 然后,可以使用以下方法在屏幕中心显示窗口。 ```python root.eval('tk::PlaceWindow %s center' % root.winfo_toplevel()) ``` 这会在窗口的位置属性中将值设置为center,从而将窗口放置在屏幕中心。 最后,使用root.mainloop()方法来显示窗口并使其保持打开状态,直到用户关闭它。 完整代码示例: ```python import tkinter as tk root = tk.Tk() root.geometry('500x500') root.eval('tk::PlaceWindow %s center' % root.winfo_toplevel()) root.mainloop() ``` 在执行此代码时,将在屏幕中心显示一个宽度和高度为500像素的窗口。这个小技巧可以在创建GUI应用程序时非常有用,使用户界面更好地呈现并保持高度可用性。 ### 回答3: Python tkinter是一个非常流行的图形用户界面(GUI)工具包,而窗口是GUI界面的主要组成部分之一,因此如何居中窗口是一个非常基础的问题。在Python tkinter中,我们可以通过以下步骤将窗口居中。 步骤1:获取屏幕的宽度和高度 ``` screen_width = root.winfo_screenwidth() # 获取屏幕宽度 screen_height = root.winfo_screenheight() # 获取屏幕高度 ``` 步骤2:获取窗口的宽度和高度 ``` win_width = root.winfo_reqwidth() # 获取窗口宽度 win_height = root.winfo_reqheight() # 获取窗口高度 ``` 步骤3:计算窗口居中的位置 ``` x = int((screen_width - win_width) / 2) # 计算窗口左上角x坐标 y = int((screen_height - win_height) / 2) # 计算窗口左上角y坐标 ``` 步骤4:将窗口移动到居中位置 ``` root.geometry("{}x{}+{}+{}".format(win_width, win_height, x, y)) # 将窗口移动到居中位置 ``` 另外,如果我们还想让窗口屏幕大小发生变化时仍然保持居中,可以将上述代码封装为函数,并将函数绑定到窗口的"Configure"事件中。 完整代码如下: ``` import tkinter as tk def center_window(root): # 获取屏幕宽度和高度 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # 获取窗口宽度和高度 win_width = root.winfo_reqwidth() win_height = root.winfo_reqheight() # 计算居中位置 x = int((screen_width - win_width) / 2) y = int((screen_height - win_height) / 2) # 将窗口移动到居中位置 root.geometry("{}x{}+{}+{}".format(win_width, win_height, x, y)) root = tk.Tk() root.title("居中窗口") center_window(root) # 将center_window函数绑定到窗口的"Configure"事件中 root.bind("<Configure>", lambda event: center_window(root)) root.mainloop() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值