tkMessageBox.showwarning 如何选择弹出的位置?

     2023-02-19     273

关键词:

【中文标题】tkMessageBox.showwarning 如何选择弹出的位置?【英文标题】:tkMessageBox.showwarning how to choose location of pop up? 【发布时间】:2018-12-26 21:51:45 【问题描述】:

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

非常感谢所有阅读的人!

【问题讨论】:

抱歉,不可能。 Tkinter 只是将您的请求传递给操作系统,操作系统决定窗口的去向。我在Linux下测试过,弹窗总是以GUI为中心;而在 Windows 上,弹出窗口始终以显示器为中心。 如果它真的很重要,您可以轻松制作自己的弹出窗口。 Here's a template. 【参考方案1】:

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

在屏幕上居中根窗口

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))

这段代码不是我写的,不记得出处了,不过还是感谢作者。

【讨论】: