Tkinter 中的笔记本小部件

发布于 2024-07-08 02:56:25 字数 464 浏览 10 评论 0原文

在使用过 Tkinter 和 wxPython 之后,我更喜欢 Tkinter,因为我的源代码看起来很干净。 然而,它似乎没有那么多功能; 特别是它没有选项卡(如 Firefox 窗口顶部的选项卡)。

对这个主题进行一些谷歌搜索可以提供一些建议。 有一个一个食谱条目,其中包含一个允许您使用选项卡的类,但它非常原始。 SourceForge 上还有 Python megawidgets,尽管这看起来很旧,并且在安装过程中给我带来了错误。

有人有在 Tkinter 中制作选项卡式 GUI 的经验吗? 你用了什么? 或者只是任何需要更强大的窗口组件的人都必须使用 wxPython?

Having played around a little with both Tkinter and wxPython, I like Tkinter much better in terms of how clean my source code looks. However, it doesn't seem to have as many features; in particular it doesn't have tabs (as in, the tabs at the top of a Firefox window).

A little Googling on the subject offers a few suggestions. There's a cookbook entry with a class allowing you to use tabs, but it's very primitive. There's also Python megawidgets on SourceForge, although this seems very old and gave me errors during installation.

Does anyone have experience making tabbed GUIs in Tkinter? What did you use? Or is it simply the case that anyone who needs more powerful windowing components has to use wxPython?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

荒岛晴空 2024-07-15 02:56:25

在最新的 Python (> 2.7) 版本中,您可以使用 ttk 模块,它提供对 Tk 主题小部件集的访问,该集已在 Tk 8.5 中引入。

以下是在 Python 2 中导入 ttk 的方法:

import ttk

help(ttk.Notebook)

在 Python 3 中,ttk 模块作为 tkinter

这是一个基于 TkDocs 中示例的简单工作示例网站:

from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText


def demo():
    root = tk.Tk()
    root.title("ttk.Notebook")

    nb = ttk.Notebook(root)

    # adding Frames as pages for the ttk.Notebook 
    # first page, which would get widgets gridded into it
    page1 = ttk.Frame(nb)

    # second page
    page2 = ttk.Frame(nb)
    text = ScrolledText(page2)
    text.pack(expand=1, fill="both")

    nb.add(page1, text='One')
    nb.add(page2, text='Two')

    nb.pack(expand=1, fill="both")

    root.mainloop()

if __name__ == "__main__":
    demo()

另一种选择是使用 中的 NoteBook 小部件tkinter.tix 库。 要使用 tkinter.tix,您必须安装 Tix 小部件,通常与安装 Tk 小部件一起安装。 要测试您的安装,请尝试以下操作:

from tkinter import tix
root = tix.Tk()
root.tk.eval('package require Tix')

有关详细信息,请查看此网页 在 PSF 网站上。

请注意,tix 已经很老了,并且没有得到很好的支持,因此您最好的选择可能是使用 ttk.Notebook

On recent Python (> 2.7) versions, you can use the ttk module, which provides access to the Tk themed widget set, which has been introduced in Tk 8.5.

Here's how you import ttk in Python 2:

import ttk

help(ttk.Notebook)

In Python 3, the ttk module comes with the standard distributions as a submodule of tkinter.

Here's a simple working example based on an example from the TkDocs website:

from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText


def demo():
    root = tk.Tk()
    root.title("ttk.Notebook")

    nb = ttk.Notebook(root)

    # adding Frames as pages for the ttk.Notebook 
    # first page, which would get widgets gridded into it
    page1 = ttk.Frame(nb)

    # second page
    page2 = ttk.Frame(nb)
    text = ScrolledText(page2)
    text.pack(expand=1, fill="both")

    nb.add(page1, text='One')
    nb.add(page2, text='Two')

    nb.pack(expand=1, fill="both")

    root.mainloop()

if __name__ == "__main__":
    demo()

Another alternative is to use the NoteBook widget from the tkinter.tix library. To use tkinter.tix, you must have the Tix widgets installed, usually alongside your installation of the Tk widgets. To test your installation, try the following:

from tkinter import tix
root = tix.Tk()
root.tk.eval('package require Tix')

For more info, check out this webpage on the PSF website.

Note that tix is pretty old and not well-supported, so your best choice might be to go for ttk.Notebook.

凹づ凸ル 2024-07-15 02:56:25

如果有人还在寻找,我已经在 tkinter 中将其作为 Tab 工作了。 尝试一下代码,使其按照您想要的方式运行(例如,您可以添加按钮来添加新选项卡):

from tkinter import *

class Tabs(Frame):

    """Tabs for testgen output"""

    def __init__(self, parent):
        super(Tabs, self).__init__()
        self.parent = parent
        self.columnconfigure(10, weight=1)
        self.rowconfigure(3, weight=1)
        self.curtab = None
        self.tabs = {}
        self.addTab()                
        self.pack(fill=BOTH, expand=1, padx=5, pady=5)

    def addTab(self):
        tabslen = len(self.tabs)
        if tabslen < 10:
            tab = {}
            btn = Button(self, text="Tab "+str(tabslen), command=lambda: self.raiseTab(tabslen))
            btn.grid(row=0, column=tabslen, sticky=W+E)

            textbox = Text(self.parent)
            textbox.grid(row=1, column=0, columnspan=10, rowspan=2, sticky=W+E+N+S, in_=self)

            # Y axis scroll bar
            scrollby = Scrollbar(self, command=textbox.yview)
            scrollby.grid(row=7, column=5, rowspan=2, columnspan=1, sticky=N+S+E)
            textbox['yscrollcommand'] = scrollby.set

            tab['id']=tabslen
            tab['btn']=btn
            tab['txtbx']=textbox
            self.tabs[tabslen] = tab
            self.raiseTab(tabslen)

    def raiseTab(self, tabid):
        print(tabid)
        print("curtab"+str(self.curtab))
        if self.curtab!= None and self.curtab != tabid and len(self.tabs)>1:
                self.tabs[tabid]['txtbx'].lift(self)
                self.tabs[self.curtab]['txtbx'].lower(self)
        self.curtab = tabid


def main():
    root = Tk()
    root.geometry("600x450+300+300")
    t = Tabs(root)
    t.addTab()
    root.mainloop()

if __name__ == '__main__':
    main()

If anyone still looking, I have got this working as Tab in tkinter. Play around with the code to make it function the way you want (for example, you can add button to add a new tab):

from tkinter import *

class Tabs(Frame):

    """Tabs for testgen output"""

    def __init__(self, parent):
        super(Tabs, self).__init__()
        self.parent = parent
        self.columnconfigure(10, weight=1)
        self.rowconfigure(3, weight=1)
        self.curtab = None
        self.tabs = {}
        self.addTab()                
        self.pack(fill=BOTH, expand=1, padx=5, pady=5)

    def addTab(self):
        tabslen = len(self.tabs)
        if tabslen < 10:
            tab = {}
            btn = Button(self, text="Tab "+str(tabslen), command=lambda: self.raiseTab(tabslen))
            btn.grid(row=0, column=tabslen, sticky=W+E)

            textbox = Text(self.parent)
            textbox.grid(row=1, column=0, columnspan=10, rowspan=2, sticky=W+E+N+S, in_=self)

            # Y axis scroll bar
            scrollby = Scrollbar(self, command=textbox.yview)
            scrollby.grid(row=7, column=5, rowspan=2, columnspan=1, sticky=N+S+E)
            textbox['yscrollcommand'] = scrollby.set

            tab['id']=tabslen
            tab['btn']=btn
            tab['txtbx']=textbox
            self.tabs[tabslen] = tab
            self.raiseTab(tabslen)

    def raiseTab(self, tabid):
        print(tabid)
        print("curtab"+str(self.curtab))
        if self.curtab!= None and self.curtab != tabid and len(self.tabs)>1:
                self.tabs[tabid]['txtbx'].lift(self)
                self.tabs[self.curtab]['txtbx'].lower(self)
        self.curtab = tabid


def main():
    root = Tk()
    root.geometry("600x450+300+300")
    t = Tabs(root)
    t.addTab()
    root.mainloop()

if __name__ == '__main__':
    main()
来世叙缘 2024-07-15 02:56:25

虽然目前它可能对您没有帮助,但 tk 8.5 附带了一组扩展的小部件。 该扩展集可通过称为“tile”的扩展在 tk 8.4 中使用。 扩展的小部件集中包括一个笔记本小部件。 不幸的是,此时 Tkinter 默认使用相当旧的 Tk 版本,该版本不附带这些小部件。

人们一直在努力让 Tkinter 可以使用 Tile。 查看http://tkinter.unpythonic.net/wiki/TileWrapper。 有关其他类似的工作,请参阅 http://pypi.python.org/pypi/pyttk。 另外,要了解这些小部件的外观(在 Ruby、Perl 和 Tcl 中),请参阅 http://www.tkdocs。 com/

Tk 8.5 比库存 Tk 有了巨大改进。 它引入了几个新的小部件、本机小部件和主题引擎。 希望不久的将来它会在 Tkinter 中默认可用。 遗憾的是,Python 世界落后于其他语言。

更新:最新版本的 Python 现在包含对开箱即用的主题小部件的支持。 _

While it may not help you at the moment, tk 8.5 comes with an extended set of widgets. This extended set is available with tk 8.4 by way of an extension known as "tile". Included in the extended set of widgets is a notebook widget. Unfortunately, at this time Tkinter by default uses a fairly old version of Tk that doesn't come with these widgets.

There have been efforts to make tile available to Tkinter. Check out http://tkinter.unpythonic.net/wiki/TileWrapper. For another similar effort see http://pypi.python.org/pypi/pyttk. Also, for a taste of how these widgets look (in Ruby, Perl and Tcl) see http://www.tkdocs.com/.

Tk 8.5 is a huge improvement over stock Tk. It introduces several new widgets, native widgets, and a theming engine. Hopefully it will be available by default in Tkinter some day soon. Too bad the Python world is lagging behind other languages.

update: The latest versions of Python now include support for the themed widgets out of the box. _

厌味 2024-07-15 02:56:25

“或者只是任何需要更强大的窗口组件的人都必须使用 wxPython?”
简短的回答:是的。

长答案:
可能需要一些练习才能让您的 wxPython 代码感觉“干净”,但它比 Tkinter 更好、更强大。 您还将获得更好的支持,因为现在越来越多的人使用它。

"Or is it simply the case that anyone who needs more powerful windowing components has to use wxPython?"
Short answer: yes.

Long answer:
It may take some practice for your wxPython code to feel "clean," but it is nicer and much more powerful than Tkinter. You will also get better support, since more people use it these days.

眼藏柔 2024-07-15 02:56:25

您在使用 pmw 时遇到了什么问题? 是的,它很旧,但它是纯 python,所以它应该可以工作。

请注意,Tix 不能与 py2exe 一起使用(如果这对您来说是个问题)。

What problems did you have with pmw? It's old, yes, but it's pure python so it should work.

Note that Tix doesn't work with py2exe, if that is an issue for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文