如何在tkinter窗口顶部添加问号[?]按钮

CDN

我想为我的python tkinter项目创建一个窗口,该窗口顶部有一个问号按钮,如下所示:

点击

反正我能做到吗?

蜥蜴

我想我可以正常使用了(目前仅适用于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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 tkinter toplevel() 窗口中添加按钮

如何在UIPageViewController顶部添加按钮?

如何在 Python/Spyder 中向新的 tkinter 窗口添加按钮?

如何在 tkinter 中向默认窗口管理器的控件添加新按钮?

如何在tkinter中的图像顶部添加文本

如何在flutter的按钮顶部添加标签

如何在clrCheckbox顶部添加selectAll / deselectAll按钮?

如何在滚动视图顶部添加浮动操作按钮

如何在 R Shiny 中添加“返回页面顶部”按钮?

如何在BottomSheetDialog android的顶部中心添加关闭按钮?

如何在jhipster中添加滚动顶部按钮?

如何在 Delphi FireMonkey 的 MapView 顶部添加可点击的按钮?

如何在同一窗口的Tkinter中正确使用按钮

如何在Tkinter窗口中动态添加/删除/更新标签?

如何在视图按钮上添加模态弹出窗口?

如何在Tkinter的子菜单中添加单选按钮

如何在 tkinter 中使用按钮添加内容?

如何在TKinter中沿着网格添加按钮

如何在python3.4 tkinter的左侧和顶部放置按钮

如何在文件前添加(在顶部添加)

如何在单选按钮系列的顶部添加简单的字符串?

在flutter中,如何在ListView的底部和顶部添加按钮

用户单击弹出窗口中的按钮后,如何在主活动中添加新按钮?

如何在xfwm4窗口按钮中添加全屏按钮?

如何在JavaPoet中添加“任何类型”问号?

nuxt 如何在页面名称后添加问号

如何在tkinter中使用grid()方法在新窗口小部件上创建按钮?

Python-Tkinter如何在按下按钮后关闭窗口?

如何在没有按钮的情况下关闭 tkinter 窗口而不完全关闭 Python?