Tkinter程序屏幕居中


本文適用場景:想用Tkinter開發界面程序並屏幕居中,但沒找到相應的API。

這兩天玩了玩Tkinter,感覺不錯,就是屏幕居中這個問題在網上搜了很長時間也沒
找到答案,最后沒辦法,看它的文檔,用自己的方法實現了。

方法很土,就是獲取初始化的窗體大小和屏幕大小,再通過計算得到大體值。
以下是代碼:

 1 #! /usr/bin/python
2 '''
3 File : screenCenter.pyw
4 Author : Mike
5 E-Mail : Mike_Zhang@live.com
6 '''
7 from Tkinter import *
8
9 rt = Tk()
10 rt.resizable(False,False)
11 rt.title("Screen center")
12
13 rt.update() # update window ,must do
14 curWidth = rt.winfo_reqwidth() # get current width
15 curHeight = rt.winfo_height() # get current height
16 scnWidth,scnHeight = rt.maxsize() # get screen width and height
17 # now generate configuration information
18 tmpcnf = '%dx%d+%d+%d'%(curWidth,curHeight,
19 (scnWidth-curWidth)/2,(scnHeight-curHeight)/2)
20 rt.geometry(tmpcnf)
21 rt.mainloop()

好,就這些了,希望對你有幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM