如何根据屏幕尺寸告诉Tkinter窗口在哪里打开?我希望它在中间打开。如何指定Tkinter窗口打开的位置?
回答
这个答案是基于Rachel's answer。她的代码原本没有工作,但通过一些调整,我能够修复错误。
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
试试这个
import tkinter as tk
def center_window(width=300, height=200):
# get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# calculate position x and y coordinates
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root = tk.Tk()
center_window(500, 400)
root.mainloop()
或参考http://eurion.net/python-snippets/snippet/Center %20window.html替代方法 – 2013-02-16 13:38:44
最大的用处,你可能想要修复你的缩进。 – mgilson 2013-02-16 13:56:19
@RachelGallen:你是否意识到代码是一个完全不同的工具包?你不能使用pyqt来定位一个tkinter窗口。 – 2013-02-16 16:31:08
root.geometry('250x150+0+0')
使用这前两个参数是该窗口的宽度和高度。最后两个参数是x和y屏幕坐标。您可以指定所需的x和y坐标
- 1. 如何打开OpenCV的窗口和Tkinter的窗口一起?
- 2. Delphi - 在TLabel的位置打开窗口
- 3. cx_freeze .exe没有打开tkinter窗口
- 4. 如何当一个新的Tkinter窗口打开
- 5. 想要用matplotlib窗口同时打开tkinter窗口吗?
- 6. 如何在鼠标单击位置打开弹出窗口
- 7. 如何在层叠位置打开一个窗口(使用MFC)
- 8. Tkinter - 我如何打开窗户?
- 9. 如何记住python 3中的tkinter窗口位置?
- 10. 如何在固定位置打开不可调整大小的弹出窗口?
- 11. 如何在Tkinter窗口中打开exe文件
- 12. Tkinter:确定相对于根窗口的Widget位置
- 13. 设置在Xvfb环境中打开窗口的默认位置
- 14. 如何将tkinter窗口置于其他窗口之上?
- 15. 在MVVM中点击的按钮的位置打开弹出窗口或窗口
- 16. 窗口打开
- 17. 何处指定窗口组件的位置?
- 18. 打开一个保留与父窗口相同位置的空白窗口
- 19. 如何使用窗口指令提示在指定程序中打开文件
- 20. 如何重新编程Vim's:help命令在指定位置打开
- 21. 如何设置WPF窗口的位置?
- 22. WPF窗口位置绑定
- 23. 绑定窗口位置
- 24. Python Tkinter:从另一个窗口打开同一窗口时的不同行为
- 25. Tkinter:打开另一个窗口的按钮(并关闭当前窗口)
- 26. 关闭窗口并通过Tkinter中的按钮打开新窗口
- 27. 如何使Tkinter窗口在打开时显示而不是开始最小化?
- 28. 打开窗口的用户是不是在指定的域
- 29. 打开的窗口与指定的宽度和高度
- 30. Tkinter自定义窗口
[an answer](http://stackoverflow.com/a/10018670/1217270) – 2013-02-17 03:34:54