Tkinter 中的笔记本小部件 [英] Notebook widget in Tkinter
问题描述
使用过 Tkinter 和 wxPython 后,我更喜欢 Tkinter,因为我的源代码看起来很干净.但是,它似乎没有那么多功能;特别是它没有标签(例如,Firefox 窗口顶部的标签).
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).
在这个主题上的一点谷歌搜索提供了一些建议.有一个食谱条目,其中包含一个允许您使用选项卡的类,但它非常原始.SourceForge 上也有 Python megawidgets,虽然这看起来很旧并且在安装过程中给了我错误.
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.
有人有在 Tkinter 中制作选项卡式 GUI 的经验吗?你用的是什么?或者只是需要更强大的窗口组件的任何人都必须使用wxPython?
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?
推荐答案
在最近的 Python (> 2.7) 版本上,您可以使用 ttk
模块,提供对 Tk 8.5 中引入的 Tk 主题小部件集的访问代码>.
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
.
以下是在 Python 2 中导入 ttk
的方法:
Here's how you import ttk
in Python 2:
import ttk
help(ttk.Notebook)
在 Python 3 中,ttk
模块作为 的子模块随标准发行版一起提供tkinter
.
In Python 3, the ttk
module comes with the standard distributions as a submodule of tkinter
.
这是一个基于 TkDocs
网站:
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()
另一种替代方法是使用 NoteBook 小部件>tkinter.tix
库.要使用 tkinter.tix
,您必须安装 Tix
小部件,通常与 Tk
小部件一起安装.要测试您的安装,请尝试以下操作:
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')
有关更多信息,请查看 PSF 网站上的此网页.
For more info, check out this webpage on the PSF website.
请注意,tix
已经很旧并且没有得到很好的支持,因此您最好的选择可能是使用 ttk.Notebook
.
Note that tix
is pretty old and not well-supported, so your best choice might be to go for ttk.Notebook
.
这篇关于Tkinter 中的笔记本小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!