Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vigilantPotato/ab992056c54efc018a7225a10f0e53df to your computer and use it in GitHub Desktop.
Save vigilantPotato/ab992056c54efc018a7225a10f0e53df to your computer and use it in GitHub Desktop.
[tkinter] <Map> event example (move main window when widget appears)
import tkinter
#Move the main window to the top-right of monitor when this button appears
class DummynButton(tkinter.Button):
def __init__(self, master):
super().__init__(
master,
width=15,
text="click",
)
self.master = master
self.bind("<Map>", self.move_position) #bind <Map> event
def move_position(self, event):
screen_width = self.winfo_screenwidth() #monior width
main_window_width = self.master.winfo_width() #tkinter main window width
frame = self.master.winfo_rootx() - self.master.winfo_x() #invisible frame width
x = screen_width - main_window_width
self.master.geometry('+%d+%d' % (x-frame, 0)) #move main window
if __name__ == "__main__":
root = tkinter.Tk()
b = DummynButton(root)
b.pack()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment