
基礎編的なgeometryメソッドの使用方法は、以下の記事を参照。
【スポンサーリンク】
メイン画面をモニターの右上隅に設定する
geometryメソッドとモニターサイズを取得するwinfo_screenwidth
やwinfo_screenwidth
を用いることで、モニターサイズに合わせて特定の位置にメイン画面を移動させることができる。
以下に、メイン画面をモニターの右上隅に移動させるボタンの作成例を示す。
winfo_screenwidth
でモニター幅を取得し、geometryメソッドでメイン画面をモニターの右上隅に移動させている。geometryメソッドに座標を渡す際は、文字列に直す必要があることに注意。
import tkinter #メイン画面をモニター右上隅に移動させるボタン class MovePositionButton(tkinter.Button): def __init__(self, master): super().__init__( master, width=15, text="click", command=self.move_position, #クリック時に実行する関数 ) self.master = master def move_position(self): w = self.winfo_screenwidth() #モニター幅取得 w = w - 120 #メイン画面幅分調整 self.master.geometry("120x100+"+str(w)+"+0") #位置設定 root = tkinter.Tk() b = MovePositionButton(root) b.pack() root.mainloop()
実行すると以下の画面が表示され、ボタンを押すとメイン画面がモニターの右上隅に移動する。


【スポンサーリンク】
その他の四隅へ移動させる場合
メイン画面をモニターのその他の位置に表示させる場合の例を示す。上記例の、move_position
メソッドを以下の様に変更すると、メイン画面の出現位置を変更することができる。
モニターの右下に表示させる場合
def move_position(self): #右下 w = self.winfo_screenwidth() #モニター横幅取得 h = self.winfo_screenheight() #モニター縦幅取得 w = w - 120 #メイン画面横幅分調整 h = h - 100 #メイン画面縦幅分調整 self.master.geometry("120x100+"+str(w)+"+"+str(h)) #位置設定
モニターの左上に表示させる場合
def move_position(self): #左上 self.master.geometry("120x100+0+0") #位置設定
モニターの左下に表示させる場合
def move_position_3(self): #左下 h = self.winfo_screenheight() h = h - 100 self.master.geometry("120x100+0+"+str(h)) #位置設定
【スポンサーリンク】