0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで画面をモニター中央に起動させる

Last updated at Posted at 2024-10-19

変更前のコード

root = tk.Tk()
root.title("温湿度計")
root.geometry("586x329")
root.configure(bg="white")

表示される位置は左上
image.png

表示位置の変更方法

表示する位置をgeometry()でサイズの後ろに指定する

root.geometry("横のサイズx縦のサイズ+左からのX軸の座標+上からのY軸の座標")

変更後のコード

下記に設定すると位置が変わる

root.geometry("586x329+1500+500")

image.png

モニターの大きさを取得して座標を指定する

# ウィンドウサイズ
window_width = 586
window_height = 329

# モニターとウィンドウの中央から座標を取得
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_coordinate = (screen_width // 2) - (window_width // 2)   # モニター中央-ウィンドウ半分
y_coordinate = (screen_height // 2) - (window_height // 2) # モニター中央-ウィンドウ半分

root.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}")
root.configure(bg="gray")

モニターの中央に表示されたのでOK!
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?