如何根据屏幕尺寸告诉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. Tkinter:窗口无法打开
- 2. Tkinter窗口没有打开
- 3. 如何打开OpenCV的窗口和Tkinter的窗口一起?
- 4. 如何打开指定的,打开的窗口中的链接
- 5. 如何在一个窗口中打开多个Tkinter窗口
- 6. 更改窗口打开的位置
- 7. 打开后新窗口的位置
- 8. GNOME-打开的窗口中定位
- 9. 蟒蛇tkinter自动打开子窗口
- 10. Python - 保持Tkinter窗口打开?
- 11. cx_freeze .exe没有打开tkinter窗口
- 12. Tkinter小部件打开两个窗口
- 13. 修改从Desktop.Browser打开的窗口的位置和位置
- 14. 如何层叠新窗口打开的位置?
- 15. 如何当一个新的Tkinter窗口打开
- 16. 想要用matplotlib窗口同时打开tkinter窗口吗?
- 17. Tkinter用窗口打开控制台窗口
- 18. 当matplotlib窗口打开时关闭tkinter进度条窗口
- 19. 如何在层叠位置打开一个窗口(使用MFC)
- 20. 如何在鼠标单击位置打开弹出窗口
- 21. Tkinter - 我如何打开窗户?
- 22. 在Screenborder上定位tkinter窗口
- 23. 在窗口中定位画布 - Tkinter/python
- 24. 如何记住python 3中的tkinter窗口位置?
- 25. 如何用按钮python 3/Tkinter打开顶层窗口
- 26. 如何让一个tkinter窗口被多次打开
- 27. 如何在Tkinter窗口中打开exe文件
- 28. 如何打开新窗口时打开父窗口
- 29. 如何在固定位置打开不可调整大小的弹出窗口?
- 30. 如何打开新窗口
[an answer](http://stackoverflow.com/a/10018670/1217270) – 2013-02-17 03:34:54