Advertisement
here2share

# tk_fullscreen_toggle.py

Mar 28th, 2023 (edited)
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # tk_fullscreen_toggle.py
  2.  
  3. import tkinter as tk
  4. import ctypes
  5. import time
  6.  
  7. # Define ctypes structures and functions
  8. class POINT(ctypes.Structure):
  9.     _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
  10.  
  11. def get_xy():
  12.     point = POINT()
  13.     ctypes.windll.user32.GetCursorPos(ctypes.byref(point))
  14.     return point.x, point.y
  15.  
  16. def gui_update(event):
  17.     global cx, cy, cw, ch
  18.     # get the root position and height
  19.     if not fullscreen:
  20.         cx = root.winfo_x()
  21.         cy = root.winfo_y()
  22.         cw = root.winfo_width()
  23.         ch = root.winfo_height()
  24.  
  25. def make_fullscreen():
  26.     global fullscreen, width, height
  27.     fullscreen = True
  28.     fullscreen_button.place_forget()
  29.     root.overrideredirect(True)
  30.     width = root.winfo_screenwidth()
  31.     height = root.winfo_screenheight()
  32.     canvas.config(width=width, height=height)
  33.     fullscreen_title_bar()
  34.  
  35. def kill_fullscreen():
  36.     global fullscreen
  37.     fullscreen = False
  38.     fs_btn()
  39.     root.overrideredirect(False)
  40.     root.geometry(f"{cx}x{cy}+{cw}+{ch}")
  41.     canvas.config(width=cw, height=ch)
  42.  
  43. def fullscreen_title_bar():
  44.     global title_bar
  45.     if y < 40:
  46.         if not title_bar:
  47.             title_bar = 1
  48.             root.overrideredirect(False)
  49.             root.geometry("%dx%d+-8+0" % (width, height-72))
  50.     else:
  51.         if title_bar:
  52.             title_bar = 0
  53.             root.overrideredirect(True)
  54.             root.geometry("%dx%d+-0+0" % (width, height))
  55.  
  56. def toggle_title_bar():
  57.     if -out > x or x > cw + out or -out > y or y > ch + out + 20:
  58.         canvas.config(bg='darkred')
  59.         root.overrideredirect(True)
  60.         fullscreen_button.place_forget()
  61.     else:
  62.         canvas.config(bg='darkgreen')
  63.         root.overrideredirect(False)
  64.         fs_btn()
  65.  
  66. ww = 300
  67. hh = 300
  68.  
  69. cx = 0
  70. cy = 0
  71.  
  72. title_bar = 1
  73. width = height = 0
  74. fullscreen = 0
  75.  
  76. root = tk.Tk()
  77. root.geometry("%dx%d+100+100" % (ww, hh))
  78. canvas = tk.Canvas(root, width=ww, height=hh, bg='darkgreen')
  79. canvas.pack()
  80.  
  81. # Create a fullscreen button widget
  82. fullscreen_button = tk.Button(canvas, text="Full Screen", command=make_fullscreen, bg="gray90")
  83. def fs_btn():
  84.     fullscreen_button.place(x=5, y=5)
  85. fs_btn()
  86.  
  87. gui_update(0)
  88. root.bind("<Configure>", gui_update)
  89.  
  90. out = 50
  91. while 1:
  92.     x, y = get_xy()
  93.     if not fullscreen:
  94.         x = (x - cx)
  95.         y = (y - cy)
  96.         toggle_title_bar()
  97.     else:
  98.         fullscreen_title_bar()
  99.    
  100.     canvas.update()
  101. 0/0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement