如何指定 Tkinter 窗口打开的位置? [英] How to specify where a Tkinter window opens?

查看:96
本文介绍了如何指定 Tkinter 窗口打开的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据屏幕尺寸告诉 Tkinter 窗口在哪里打开?我希望它在中间打开.

How can I tell a Tkinter window where to open, based on screen dimensions? I would like it to open in the middle.

推荐答案

此答案基于 Rachel 的答案.她的代码最初不起作用,但通过一些调整我能够修复错误.

This answer is based on Rachel's answer. Her code did not work originally, but with some tweaking I was able to fix the mistakes.

import tkinter as tk


root = tk.Tk() # create a Tk root window

w = 800 # width for the Tk root
h = 650 # height for the Tk root

# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen

# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

# set the dimensions of the screen 
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

root.mainloop() # starts the mainloop

这篇关于如何指定 Tkinter 窗口打开的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆