Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Last active April 19, 2023 13:45
Show Gist options
  • Save vigilantPotato/813262685177063645aa95c3959a116d to your computer and use it in GitHub Desktop.
Save vigilantPotato/813262685177063645aa95c3959a116d to your computer and use it in GitHub Desktop.
how to just fit tkinter main window to the edge of display
import ctypes
import tkinter
"""Note
Tkinter main window has invisible frame around it.
When you want to just fit the main window to the edge of display,
you have to consider the width of the invisible frame.
For example, when you use root.geometry("+0+100"),
the main window will not be positioned to your left side of display.
You can get the frame width using "root.winfo_rootx() - root.winfo_x()".
- winfo_rootx method: get x position of main window without frame
- winfo_x method: get x position of the invisible frame
"""
class MoveToLeftEdgeButton(tkinter.Button):
"""
button widget to just fit the main window
to the left edge of display
"""
def __init__(self, root):
self.root = root
super().__init__(
root,
text="Move to left edge",
command=self.move_to_left_edge,
bg='lightblue',
width=20,
)
self.pack()
def move_to_left_edge(self):
"""
if you give x=0 to geometry method,
you will see some gap between main window and display left edge.
you have to set -1*frame to geometry method.
"""
frame = self.root.winfo_rootx() - self.root.winfo_x() #invisible frame width
self.root.geometry('+%d+%d' % (-1*frame, 0))
class MoveToRightEdgeButton(tkinter.Button):
"""
button widget to just fit the main window
to the right edge of display
"""
def __init__(self, root):
self.root = root
super().__init__(
root,
text="Move to right edge",
command=self.move_to_right_edge,
bg="lightgreen",
width=20,
)
self.pack()
def move_to_right_edge(self):
"""
if you give (x = winfo_screenwidth - winfo_width) to geometry method,
the right side of the main window will be hidden.
you have to set x-frame to geometry method.
"""
frame = self.root.winfo_rootx() - self.root.winfo_x() #invisible frame width
x = self.root.winfo_screenwidth() - self.root.winfo_width()
self.root.geometry('+%d+%d' % (x - frame, 0))
class MoveToLeftEdgeButton_NG(tkinter.Button):
"""
NG example
you will see some gap
"""
def __init__(self, root):
self.root = root
super().__init__(
root,
text="Move to left edge_NG",
command=self.move_to_left_edge,
bg='pink',
width=20,
)
self.pack()
def move_to_left_edge(self):
"""
give x=0 to geometry method
"""
self.root.geometry('+%d+%d' % (0, 0))
class MoveToRightEdgeButton_NG(tkinter.Button):
"""
NG example
the right side of the main window will be hidden.
"""
def __init__(self, root):
self.root = root
super().__init__(
root,
text="Move to right edge_NG",
command=self.move_to_right_edge,
bg='pink',
width=20,
)
self.pack()
def move_to_right_edge(self):
"""
give (x = winfo_screenwidth - winfo_width) to geometry method
"""
x = self.root.winfo_screenwidth() - self.root.winfo_width()
self.root.geometry('+%d+%d' % (x, 0))
if __name__ == "__main__":
ctypes.windll.shcore.SetProcessDpiAwareness(1) #resolution setting
root = tkinter.Tk()
b_left = MoveToLeftEdgeButton(root)
b_left_ng = MoveToLeftEdgeButton_NG(root)
b_right = MoveToRightEdgeButton(root)
b_right_ng = MoveToRightEdgeButton_NG(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment