在 tkinter 中添加笔记本选项卡 - 如何使用基于 class 的结构来实现? (Python 2)

Adding notebook tabs in tkinter - how do I do it with a class-based structure? (Python 2)

我希望每个选项卡都来自它自己的 class(classes 在它们自己的文件中 - 我现在只是测试第一个)。

这是我尝试过的:

tab1.py

from Tkinter import *
import Tkinter as tk

class Tab(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        fr = Frame(self).pack()
        Label(fr, text="one", bg='red', bd=2).pack()
        Label(fr, text="two", bg='yellow', bd=2).pack()

if __name__ == '__main__':
    root = Tk()
    frame = Frame(root).pack()
    Tab(frame)
    Button(frame, text='only if class', command=root.destroy).pack()
    mainloop()

noteBook.py

from Tkinter import *
from ttk import *
from tab1 import Tab

root = Tk()
note = Notebook(root)

main_frame = Frame(note)
button1 = Button(main_frame, text='test').pack()

#tab1 = Tab(note)
tab1 = Frame(note)
tab2 = Frame(note)
tab3 = Frame(note)
Tab(tab1)
Button(tab1, text='Exit', command=root.destroy).pack()

note.add(tab1, text = "Tab One", compound=TOP)
note.add(tab2, text = "Tab Two")
note.add(tab3, text = "Tab Three")
note.pack()
root.mainloop()
exit()

运行 与:

python2.7 noteBook.py

问题是 tab1.py 的内容没有出现在第一个选项卡中,而是出现在包含整个笔记本的框架中。

另外,当 运行ning tab1.py 直接与 python2.7 noteBook.py 一起使用时,我需要它表现得正确,这意味着它现在应该只显示带有来自 if __name___...部分。

我遇到了多个例子,但只找到了一个我想要的,但它没有有效的解决方案,它适用于 python3 - 我想要 python2。 python3 question with no working answer谢谢。

问题是这行代码:

fr = Frame(self).pack()

当你执行上述操作时,frNone 因为 .pack() returns None (因为 x().y() returns y() 的值)。稍后,你这样做:

Label(fr, text="one", bg='red', bd=2).pack()

因为 frNone,标签是在根 window 中创建的。

与问题无关,这里有一些建议:您创建的框架太多。 Tab 内不需要 fr,也不需要 tab1tab2tab3

这就是 Tab 所需的一切:

class Tab(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master, background="pink")
        Label(self, text="one", bg='red', bd=2).pack()
        Label(self, text="two", bg='yellow', bd=2).pack()

要将其添加到笔记本中,只需两行:

tab1 = Tab(note)
note.add(tab1, text = "Tab One", compound=TOP)

这非常有效,只是为了好玩我已经说明了选项卡 2 和 3 的填充,尽管我只是为了简单起见重复使用了相同的 class。目标是能够 运行 选项卡直接在开发过程中单独查看它们,而不必每次都 运行 整个事情。

noteBook.py

from Tkinter import *
from ttk import *
from tab1 import Tab

root = Tk()
note = Notebook(root)

main_frame = Frame(note)
button1 = Button(main_frame, text='test').pack()

tab1 = Frame(note)
tab2 = Frame(note)
tab3 = Frame(note)
Tab(tab1)
Tab(tab2)
Tab(tab3)
Button(tab1, text='Exit', command=root.destroy).pack()

note.add(tab1, text = "Tab One", compound=TOP)
note.add(tab2, text = "Tab Two")
note.add(tab3, text = "Tab Three")
note.pack()
root.mainloop()
exit()

tab1.py

import Tkinter as tk

class Tab(tk.Frame):
    def __init__(self, parent_widget):
        tk.Frame.__init__(self, parent_widget)
        self.fr = tk.Frame(parent_widget, width=200, height=200, bg='pink', bd=2)
        tk.Label(self.fr, text="one", bg='red', bd=2).pack()
        tk.Label(self.fr, text="two", bg='yellow', bd=2).pack()
        self.fr.pack() # this packing must be done after 2 above packings

if __name__ == '__main__':
    root = tk.Tk() # the app window
    main_frame = tk.Frame(root, height=200, width=200, bg='blue', bd=2) # main frame
    Tab(main_frame) # instatiate Tab(), sending main_frame as the parent_widget
    tk.Button(main_frame, text='only if class', command=root.destroy).pack()
    main_frame.pack() # display main frame on window
    tk.mainloop()