Tkinterでwindowを画面中央に置く方法を教えてください。
どんなに調べても出てこなかったので...
python2.7
macos10.12.6
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
Win10+Python3.5
上では以下を参考にして実現できました。
基本的には2.7
でも同様の手法で実現できると思います。
How to center a window on the screen in Tkinter?
Python
1import tkinter # Python 3 2 3def center(win): 4 """ 5 centers a tkinter window 6 :param win: the root or Toplevel window to center 7 """ 8 win.update_idletasks() 9 width = win.winfo_width() 10 frm_width = win.winfo_rootx() - win.winfo_x() 11 win_width = width + 2 * frm_width 12 height = win.winfo_height() 13 titlebar_height = win.winfo_rooty() - win.winfo_y() 14 win_height = height + titlebar_height + frm_width 15 x = win.winfo_screenwidth() // 2 - win_width // 2 16 y = win.winfo_screenheight() // 2 - win_height // 2 17 win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) 18 win.deiconify() 19 20root = tkinter.Tk() 21root.attributes('-alpha', 0.0) # 非表示 22center(root) 23root.attributes('-alpha', 1.0) # 表示 24root.mainloop() 25
投稿2017/08/11 07:06
編集2017/08/11 07:07総合スコア38266
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/11 08:24