Advertisement
here2share

# Tk_active_titlebar.py

Dec 25th, 2021
1,507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. # Tk_active_titlebar.py
  2.  
  3. import tkinter as tk
  4.  
  5. # --- constants --- (UPPER_CASE_NAMES)
  6.  
  7. # title bar colors
  8. TITLE_FOREGROUND = "white"
  9. TITLE_BACKGROUND = "#2c2c2c"
  10. TITLE_BACKGROUND_HOVER = "green"
  11.  
  12. BUTTON_FOREGROUND = "white"
  13. BUTTON_BACKGROUND = TITLE_BACKGROUND
  14. BUTTON_FOREGROUND_HOVER = BUTTON_FOREGROUND
  15. BUTTON_BACKGROUND_HOVER = 'red'
  16.  
  17. # window colors
  18. WINDOW_BACKGROUND = "white"
  19. WINDOW_FOREGROUND = "black"
  20.  
  21. # --- classes --- (CamelCaseNames)
  22.  
  23. class MyButton(tk.Button):
  24.  
  25.     def __init__(self, master, text='x', command=None, **kwargs):
  26.         super().__init__(master, bd=0, font="bold", padx=5, pady=2,
  27.                          fg=BUTTON_FOREGROUND,
  28.                          bg=BUTTON_BACKGROUND,
  29.                          activebackground=BUTTON_BACKGROUND_HOVER,
  30.                          activeforeground=BUTTON_FOREGROUND_HOVER,
  31.                          highlightthickness=0,
  32.                          text=text,
  33.                          command=command)
  34.  
  35.         self.bind('<Enter>', self.on_enter)
  36.         self.bind('<Leave>', self.on_leave)
  37.  
  38.     def on_enter(self, event):
  39.         self['bg'] = BUTTON_BACKGROUND_HOVER
  40.  
  41.     def on_leave(self, event):
  42.         self['bg'] = BUTTON_BACKGROUND
  43.  
  44. class MyTitleBar(tk.Frame):
  45.  
  46.     def __init__(self, master, *args, **kwargs):
  47.         super().__init__(master, relief='raised', bd=1,
  48.                          bg=TITLE_BACKGROUND,
  49.                          highlightcolor=TITLE_BACKGROUND,
  50.                          highlightthickness=0)
  51.  
  52.         self.title_label = tk.Label(self,
  53.                                     bg=TITLE_BACKGROUND,
  54.                                     fg=TITLE_FOREGROUND)
  55.                                    
  56.         self.set_title("Title Name")
  57.  
  58.         self.close_button = MyButton(self, text='x', command=master.destroy)
  59.         self.minimize_button = MyButton(self, text='-', command=self.on_minimize)
  60.         self.other_button = MyButton(self, text='#', command=self.on_other)
  61.                          
  62.         self.pack(expand=True, fill='x')
  63.         self.title_label.pack(side='left')
  64.         self.close_button.pack(side='right')
  65.         self.minimize_button.pack(side='right')
  66.         self.other_button.pack(side='right')
  67.  
  68.         self.bind("<ButtonPress-1>", self.on_press)
  69.         self.bind("<ButtonRelease-1>", self.on_release)
  70.         self.bind("<B1-Motion>", self.on_move)
  71.        
  72.     def set_title(self, title):
  73.         self.title = title
  74.         self.title_label['text'] = title
  75.        
  76.     def on_press(self, event):
  77.         self.xwin = event.x
  78.         self.ywin = event.y
  79.         self.set_title("Title Name - ... I'm moving! ...")
  80.         self['bg'] = 'green'
  81.         self.title_label['bg'] = TITLE_BACKGROUND_HOVER
  82.  
  83.     def on_release(self, event):
  84.         self.set_title("Title Name")
  85.         self['bg'] = TITLE_BACKGROUND
  86.         self.title_label['bg'] = TITLE_BACKGROUND
  87.        
  88.     def on_move(self, event):
  89.         x = event.x_root - self.xwin
  90.         y = event.y_root - self.ywin
  91.         self.master.geometry(f'+{x}+{y}')
  92.        
  93.     def on_minimize(self):
  94.         print('TODO: minimize')
  95.                
  96.     def on_other(self):
  97.         print('TODO: other')
  98.  
  99. # --- functions ---
  100.  
  101. # empty
  102.  
  103. # --- main ---
  104.  
  105. root = tk.Tk()
  106. # turns off title bar, geometry
  107. root.overrideredirect(True)
  108.  
  109. # set new geometry
  110. root.geometry('400x100+200+200')
  111.  
  112. title_bar = MyTitleBar(root)
  113. #title_bar.pack()  # it is inside `TitleBar.__init__()`
  114.  
  115. # a canvas for the main area of the window
  116. window = tk.Canvas(root, bg=WINDOW_BACKGROUND, highlightthickness=0)
  117.  
  118. # pack the widgets
  119. window.pack(expand=True, fill='both')
  120.  
  121. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement