我想我可以正常使用了(目前仅适用于Windows):
import tkinter as tk
from tkinter import ttk
class BetterTk(tk.Frame):
def __init__(self, show_close=True, show_minimise=True,
show_questionmark=False, show_fullscreen=True,
highlightthickness=3, **kwargs):
# Set up the window
self.root = tk.Tk(**kwargs)
self.root.overrideredirect(True)
self.root.bind("<Map>", self.check_map)
self.master_frame = tk.Frame(self.root, highlightbackground="grey",
highlightthickness=1)
self.master_frame.pack(expand=True, fill="both")
# The callback for when the "?" is pressed
self._question = None
super().__init__(self.master_frame)
super().pack(expand=True, side="bottom", fill="both")
# Set up the title bar frame
self.title_bar = tk.Frame(self.master_frame, bg="white")
self.title_bar.pack(side="top", fill="x")
# Add a separator
self.separator = ttk.Separator(self.master_frame)
self.separator.pack(fill="x", pady=5)
# Set up the variables for dragging the window
self._offsetx = 0
self._offsety = 0
self.dragging = False
self.root.bind("<Button-1>", self.clickwin)
self.root.bind("<ButtonRelease-1>", self.stopclickwin)
self.root.bind("<B1-Motion>", self.dragwin)
self.title_frame = tk.Frame(self.title_bar, bg="white")
self.buttons_frame = tk.Frame(self.title_bar, bg="white")
self.title_frame.pack(expand=True, side="left", anchor="w", padx=5)
self.buttons_frame.pack(expand=True, side="right", anchor="e")
self.title_label = tk.Label(self.title_frame, text="Better Tk",
bg="white")
self.title_label.grid(row=1, column=1, sticky="news")
# Set up all of the buttons
self.buttons = []
column = 1
if show_questionmark:
button = tk.Button(self.buttons_frame, text="?", relief="flat",
command=self.question, bg="white")
button.grid(row=1, column=column)
self.buttons.append(button)
column += 1
if show_minimise:
button = tk.Button(self.buttons_frame, text="_", relief="flat",
command=self.minimise, bg="white")
button.grid(row=1, column=column)
self.buttons.append(button)
column += 1
if show_fullscreen:
button = tk.Button(self.buttons_frame, text="[]", relief="flat",
command=self.fullscreen, bg="white")
button.grid(row=1, column=column)
self.fullscreen_button = button
self.buttons.append(button)
column += 1
if show_close:
button = tk.Button(self.buttons_frame, text="X", relief="flat",
command=self.close, bg="white")
button.grid(row=1, column=column)
self.buttons.append(button)
column += 1
def title(self, title):
# Changing the title of the window
self.title_label.config(text=title)
def geometry(self, *args, **kwargs):
self.root.geometry(*args, **kwargs)
def question(self):
if self._question is not None:
self._question()
def close(self):
self.root.destroy()
def minimise(self):
self.root.withdraw()
self.root.overrideredirect(False)
self.root.iconify()
def check_map(self, event):
# Whenever the user clicks on the window from the Windows bar
# Kindly plagiarised from:
# https://stackoverflow.com/a/52720802/11106801
self.root.overrideredirect(True)
def fullscreen(self):
# This toggles between the `fullscreen` and `notfullscreen` methods
self.fullscreen_button.config(command=self.notfullscreen)
self.root.overrideredirect(False)
self.root.attributes("-fullscreen", True)
def notfullscreen(self):
# This toggles between the `fullscreen` and `notfullscreen` methods
self.fullscreen_button.config(command=self.fullscreen)
self.root.attributes("-fullscreen", False)
self.root.overrideredirect(True)
def dragwin(self, event):
# If started dragging:
if self.dragging:
x = self.root.winfo_pointerx() - self._offsetx
y = self.root.winfo_pointery() - self._offsety
# Move to the cursor's location
self.root.geometry("+%d+%d"%(x, y))
def stopclickwin(self, event):
self.dragging = False
def clickwin(self, event):
# Get the widget that was pressed:
widget = event.widget
# Check if it is part of the title bar or something else
# It checks its parent and its parent's parent and
# its parent's parent's parent and ... until it finds
# whether or not the widget clicked on is the title bar.
while widget != self.root:
if widget == self:
# If it isn't the title bar just stop
return None
widget = widget.master
# If it is the title bar start dragging:
self.dragging = True
self._offsetx = event.x+event.widget.winfo_rootx()-\
self.root.winfo_rootx()
self._offsety = event.y+event.widget.winfo_rooty()-\
self.root.winfo_rooty()
root = tk.Tk()
root = BetterTk(show_questionmark=True)
root.title("tk")
def function():
print("question was pressed")
root._question = function
label1 = tk.Label(root, text="-"*10+" This is the better Tk "+"-"*10)
label1.grid(row=1, column=1)
label2 = tk.Label(root, text="-"*10+" This is the better Tk "+"-"*10)
label2.grid(row=2, column=1)
root.mainloop()
我删除从标题栏tk.Tk
使用.overrideredirect(True)
。之后,我创建了自己的标题栏并将其放在顶部。使用此方法,您可以根据需要添加任意数量的按钮。我还使标题栏可拖动,以便您可以移动窗口周围。
注意:您不能使用大多数常规tk.Tk
方法。如果您需要使用它们,那就做吧root.root.<method>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句