子类化Tkinter以创建自定义Widget

use*_*098 11 python inheritance class tkinter widget

请看下面的代码:(我基本上尝试使用垂直滚动条创建一个文本小部件,同时保留我班级中Tkinter.Text的所有方法/函数)

class ScrollableTextWidget(Tkinter.Text):
    def __init__(self, parent):
        self.parent = parent
        self.Frame = ttk.Frame(self.parent)
        Tkinter.Text.__init__(self, self.Frame, width=1, height=1)
        self.__initWidget()

    def __initWidget(self):
        self.Frame.grid(sticky="NSEW")
        self.ScrollbarY = ttk.Scrollbar(self.Frame, orient="vertical", command=self.yview)
        self.configure(yscrollcommand=self.ScrollbarY.set)
        self.grid(column=0, row=0, sticky="NSEW")
        self.ScrollbarY.grid(column=1, row=0, sticky="NS")
        self.Frame.columnconfigure(0, weight=1)
        self.Frame.rowconfigure(0, weight=1)
Run Code Online (Sandbox Code Playgroud)

可以像这样创建我的自定义小部件,还是应该把它放在一个框架中并编写我自己的方法?

Bry*_*ley 24

将窗口小部件子类化以创建自定义窗口小部件是非常正常的.但是,如果此自定义窗口小部件由多个窗口小部件组成,则通常会进行子类化Frame.例如,要创建一个带有滚动条的文本小部件的小部件,我会这样做:

import Tkinter as tk

class ScrolledText(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent)
        self.text = tk.Text(self, *args, **kwargs)
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.scrolled_text = ScrolledText(self)
        self.scrolled_text.pack(side="top", fill="both", expand=True)
        with open(__file__, "r") as f:
            self.scrolled_text.text.insert("1.0", f.read())

root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

使用此方法,请注意在插入文本时如何引用内部文本小部件.如果您希望此窗口小部件看起来更像真实文本窗口小部件,则可以创建到部分或全部文本窗口小部件功能的映射.例如:

import Tkinter as tk

class ScrolledText(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent)
        self.text = tk.Text(self, *args, **kwargs)
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

        # expose some text methods as methods on this object
        self.insert = self.text.insert
        self.delete = self.text.delete
        self.mark_set = self.text.mark_set
        self.get = self.text.get
        self.index = self.text.index
        self.search = self.text.search

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.scrolled_text = ScrolledText(self)
        self.scrolled_text.pack(side="top", fill="both", expand=True)
        with open(__file__, "r") as f:
            self.scrolled_text.insert("1.0", f.read())

root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

  • 您的解决方案的问题恰恰在于您必须手动公开文本成员方法,而OP希望免费获得此方法.因此,我猜测他最初的继承Text的想法是正确的方法.我的Tkinter掌握太浅而无法回答这个问题,但如果有人能这样做,我会很高兴. (2认同)

Mic*_*ian 2

我会在中型、大型项目或将来支持的项目中使用子类,因为子类可以轻松替换或更新。无论如何,子类化的成本很小。但如果你的任务是一次性的,那就肮脏而快速。

pack在这种情况下,我将从 Frame 进行子类化,因为它是最通用的小部件,不需要 overridegrid和其他方法。