首页 > 解决方案 > tkMessageBox.showwarning 如何选择弹出位置?

问题描述

我知道您可以使用几何来确定 tkinter 中窗口或顶层的位置,但是对于 tkMessageBox 呢?我有一个带有小 GUI 的程序,它通常出现在屏幕的左上角,但弹出窗口总是射到屏幕中间。我只需要一种在弹出窗口中使用几何(或类似的东西)的方法。

非常感谢所有阅读的人!

标签: pythontkinter

解决方案


上周我正在研究这个并找到了这个解决方案,它可以完美地工作并且很容易包含在您的代码中。

在屏幕上居中根窗口

from tkinter import * 
root = Tk()


# Gets the requested values of the height and width.
windowWidth = root.winfo_reqwidth()
windowHeight = root.winfo_reqheight()

# Gets both half the screen width/height and window width/height
positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)

# Positions the window in the center of the page.
root.geometry("+{}+{}".format(positionRight, positionDown))

这段代码不是我写的,也不记得是从哪里来的,不过还是感谢作者。


推荐阅读