Python 在多个选项卡中使用滚动条

Python 在多个选项卡中使用滚动条,python,tkinter,scrollbar,Python,Tkinter,Scrollbar,我创建了一个多页面程序(在Mac上使用Python2.7和Tkinter),页面之间有导航按钮。我希望能够在某些页面上使用滚动条,但当我为每个页面输入滚动条代码时,只有添加了滚动条代码的最后一个页面才是可滚动的。滚动条工作得很好(我知道如何制作),但似乎一次只有一个页面可以有一个工作的滚动条。为什么?这个确切的问题(几乎)在几年前在这里被问过(),但一直没有得到回答。我也尝试过将滚动条代码放入“主窗口”和“创建新文件”类代码中,但奇怪的事情发生了。以下是我尝试做的一个基本版本: from Tki

我创建了一个多页面程序(在Mac上使用Python2.7和Tkinter),页面之间有导航按钮。我希望能够在某些页面上使用滚动条,但当我为每个页面输入滚动条代码时,只有添加了滚动条代码的最后一个页面才是可滚动的。滚动条工作得很好(我知道如何制作),但似乎一次只有一个页面可以有一个工作的滚动条。为什么?这个确切的问题(几乎)在几年前在这里被问过(),但一直没有得到回答。我也尝试过将滚动条代码放入“主窗口”和“创建新文件”类代码中,但奇怪的事情发生了。以下是我尝试做的一个基本版本:

from Tkinter import *

def quit(): #quits the program
    master.destroy()

class FirstPage(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        #creating vertical and horizontal scrollbars
        self.canvas = Canvas(self, background = "#ffffff", 
            highlightcolor = "white")
        self.canvas.pack(side = "left", fill = "both", anchor = "center",
            expand = True)

        self.vscrollbar = Scrollbar(self, orient = "vertical", 
            command = self.canvas.yview)
        self.vscrollbar.pack(side = "right", fill = "y")
        self.canvas.configure(yscrollcommand = self.vscrollbar.set)

        self.hscrollbar = Scrollbar(self, orient = "horizontal", 
            command = self.canvas.xview)
        self.hscrollbar.pack(side = "bottom", fill = "x")
        self.canvas.configure(xscrollcommand = self.hscrollbar.set)

        self.container = Frame(self.canvas, highlightcolor = "white")

        self.canvas.create_window(0, 0, window = self.container, 
            anchor = "center")

        self.container.bind("<Configure>", self.onFrameConfigure)
        self.canvas.bind_all("<MouseWheel>", self.on_vertical)
        self.canvas.bind_all("<Shift-MouseWheel>", self.on_horizontal)


        self.label = Label(self.container, text = "Welcome!")
        self.label.pack(side = "top", fill = "both", expand = False)

    def onFrameConfigure(self, event):
        #Reset the scroll region to encompass the inner frame
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

    def on_vertical(self, event):
        self.canvas.yview_scroll(-1 * event.delta, 'units')
        #lets the user use the mouse/trackpad to vertically scroll

    def on_horizontal(self, event):
        self.canvas.xview_scroll(-1 * event.delta, 'units')
        #lets the user use the shift-mouse/trackpad to horizontally scroll

class SecondPage(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        #creating vertical and horizontal scrollbars
        self.canvas = Canvas(self, background = "#ffffff", 
            highlightcolor = "white")
        self.canvas.pack(side = "left", fill = "both", anchor = "center",
            expand = True)

        self.vscrollbar = Scrollbar(self, orient = "vertical", 
            command = self.canvas.yview)
        self.vscrollbar.pack(side = "right", fill = "y")
        self.canvas.configure(yscrollcommand = self.vscrollbar.set)

        self.hscrollbar = Scrollbar(self, orient = "horizontal", 
            command = self.canvas.xview)
        self.hscrollbar.pack(side = "bottom", fill = "x")
        self.canvas.configure(xscrollcommand = self.hscrollbar.set)

        self.container = Frame(self.canvas, highlightcolor = "white")

        self.canvas.create_window(0, 0, window = self.container, 
            anchor = "center")

        self.container.bind("<Configure>", self.onFrameConfigure)
        self.canvas.bind_all("<MouseWheel>", self.on_vertical)
        self.canvas.bind_all("<Shift-MouseWheel>", self.on_horizontal)


        self.label = Label(self.container, text = "Hello World!")
        self.label.pack(side = "top", fill = "both", expand = False)

    def onFrameConfigure(self, event):
        #Reset the scroll region to encompass the inner frame
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

    def on_vertical(self, event):
        self.canvas.yview_scroll(-1 * event.delta, 'units')
        #lets the user use the mouse/trackpad to vertically scroll

    def on_horizontal(self, event):
        self.canvas.xview_scroll(-1 * event.delta, 'units')
        #lets the user use the shift-mouse/trackpad to horizontally scroll


class ThirdPage(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        #creating vertical and horizontal scrollbars
        self.canvas = Canvas(self, background = "#ffffff", 
            highlightcolor = "white")
        self.canvas.pack(side = "left", fill = "both", anchor = "center",
            expand = True)

        self.vscrollbar = Scrollbar(self, orient = "vertical", 
            command = self.canvas.yview)
        self.vscrollbar.pack(side = "right", fill = "y")
        self.canvas.configure(yscrollcommand = self.vscrollbar.set)

        self.hscrollbar = Scrollbar(self, orient = "horizontal", 
            command = self.canvas.xview)
        self.hscrollbar.pack(side = "bottom", fill = "x")
        self.canvas.configure(xscrollcommand = self.hscrollbar.set)

        self.container = Frame(self.canvas, highlightcolor = "white")

        self.canvas.create_window(0, 0, window = self.container, 
            anchor = "center")

        self.container.bind("<Configure>", self.onFrameConfigure)
        self.canvas.bind_all("<MouseWheel>", self.on_vertical)
        self.canvas.bind_all("<Shift-MouseWheel>", self.on_horizontal)


        self.label = Label(self.container, text = "Hello World 2.0!")
        self.label.pack(side = "top", fill = "both", expand = False)

    def onFrameConfigure(self, event):
        #Reset the scroll region to encompass the inner frame
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

    def on_vertical(self, event):
        self.canvas.yview_scroll(-1 * event.delta, 'units')
        #lets the user use the mouse/trackpad to vertically scroll

    def on_horizontal(self, event):
        self.canvas.xview_scroll(-1 * event.delta, 'units')
        #lets the user use the shift-mouse/trackpad to horizontally scroll


class CreateNewFile(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

    """ If the scrollbar code goes here (and container is deleted then 
        replaced with self.container), then the buttonframe gets pushed to 
        the bottom, and the two buttons do not work (I cannot see the 
        pages they create). The scrollbar also doesn't work.
    """

        #the pages the buttons will navigate to
        secondpage = SecondPage(self)
        thirdpage = ThirdPage(self)

        #creating the navigation bar vs. the window
        buttonframe = Frame(self)
        buttonframe.pack(side = "top", fill = "x", anchor = "w", expand = 
            False)

        #creating the window container
        container = Frame(self)
        container.pack(side = "top", fill = "both", expand = True)

        #placing the pages in the container
        secondpage.place(in_ = container, x = 0, y = 0, relwidth = 1,
            relheight = 1)
        thirdpage.place(in_ = container, x = 0, y = 0, relwidth = 1,
            relheight = 1)

        #placing the buttons in the navigation bar
        secondpagebutton = Button(buttonframe, text = "2nd Page", command =         
            secondpage.lift)
        secondpagebutton.pack(side = "left") 

        thirdpagebutton = Button(buttonframe, text = "3rd Page", command = 
            thirdpage.lift)
        thirdpagebutton.pack(side = "left") 



class MainWindow(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        #the pages the buttons will nagivate to
        firstpage = FirstPage(self)
        createnewfile = CreateNewFile(self)

    """ If the scrollbar code goes here (and container is deleted then 
        replaced with self.container), then the buttonframe gets pushed to 
        the bottom, and the the createnewfilebutton does not work (I 
        cannot see the page it creates). The scrollbar also doesn't work.
    """

        #creating the button navigation bar and the rest of the window so the 
        #buttons are always visible no matter which page you're on
        buttonframe = Frame(self)
        buttonframe.pack(side = "top", fill = "x", expand = False)

        #creating the window container
        container = Frame(self)
        container.pack(side = "top", fill = "both", expand = True)

        #placing the pages in the container
        firstpage.place(in_ = container, x = 0, y = 0, relwidth = 1,
            relheight = 1)
        createnewfile.place(in_ = container, x = 0, y = 0, relwidth = 1,
            relheight = 1)

        #placing the buttons in the navigation bar
        quitbutton = Button(buttonframe, text = "Quit", command = quit)
        quitbutton.pack(side = "left") #this quits the whole program 

        createnewfilebutton = Button(buttonframe, text = "Create New File",
            command = createnewfile.lift)
        createnewfilebutton.pack(side = "left")

        firstpage.lift()

if __name__ == "__main__":
    master = Tk()
    main = MainWindow(master)
    main.pack(side = "top", fill = "both", expand = True)
    main.master.title("Basic Example")
    master.wm_geometry("600x500+0+0")
    master.mainloop()
从Tkinter导入*
def quit():#退出程序
毁灭大师
类首页(框架):
定义初始化(self,*args,**kwargs):
帧._uu初始化(self,*args,**kwargs)
#创建垂直和水平滚动条
self.canvas=canvas(self,background=“#ffffff”,
highlightcolor=“白色”)
self.canvas.pack(side=“left”、fill=“both”、anchor=“center”,
expand=True)
self.vscrollbar=滚动条(self,orient=“vertical”,
command=self.canvas.yview)
self.vscrollbar.pack(side=“right”,fill=“y”)
self.canvas.configure(yscrollcommand=self.vscrollbar.set)
self.hscrollbar=滚动条(self,orient=“horizontal”,
command=self.canvas.xview)
self.hscrollbar.pack(side=“bottom”,fill=“x”)
configure(xscrollcommand=self.hscrollbar.set)
self.container=Frame(self.canvas,highlightcolor=“白色”)
self.canvas.create_窗口(0,0,window=self.container,
anchor=“中心”)
self.container.bind(“,self.onFrameConfigure)
self.canvas.bind_all(“,self.on_垂直)
self.canvas.bind_all(“,self.on_水平)
self.label=label(self.container,text=“欢迎!”)
self.label.pack(side=“top”,fill=“both”,expand=False)
def onFrameConfigure(自我,事件):
#重置滚动区域以包围内部框架
self.canvas.configure(scrollregion=self.canvas.bbox(“全部”))
def on_垂直(自身、事件):
self.canvas.yview\u滚动(-1*event.delta,“units”)
#允许用户使用鼠标/轨迹板垂直滚动
def位于水平位置(自身、事件):
self.canvas.xview_滚动(-1*event.delta,'units'))
#允许用户使用shift鼠标/轨迹板水平滚动
课堂第二页(框架):
定义初始化(self,*args,**kwargs):
帧._uu初始化(self,*args,**kwargs)
#创建垂直和水平滚动条
self.canvas=canvas(self,background=“#ffffff”,
highlightcolor=“白色”)
self.canvas.pack(side=“left”、fill=“both”、anchor=“center”,
expand=True)
self.vscrollbar=滚动条(self,orient=“vertical”,
command=self.canvas.yview)
self.vscrollbar.pack(side=“right”,fill=“y”)
self.canvas.configure(yscrollcommand=self.vscrollbar.set)
self.hscrollbar=滚动条(self,orient=“horizontal”,
command=self.canvas.xview)
self.hscrollbar.pack(side=“bottom”,fill=“x”)
configure(xscrollcommand=self.hscrollbar.set)
self.container=Frame(self.canvas,highlightcolor=“白色”)
self.canvas.create_窗口(0,0,window=self.container,
anchor=“中心”)
self.container.bind(“,self.onFrameConfigure)
self.canvas.bind_all(“,self.on_垂直)
self.canvas.bind_all(“,self.on_水平)
self.label=label(self.container,text=“Hello World!”)
self.label.pack(side=“top”,fill=“both”,expand=False)
def onFrameConfigure(自我,事件):
#重置滚动区域以包围内部框架
self.canvas.configure(scrollregion=self.canvas.bbox(“全部”))
def on_垂直(自身、事件):
self.canvas.yview\u滚动(-1*event.delta,“units”)
#允许用户使用鼠标/轨迹板垂直滚动
def位于水平位置(自身、事件):
self.canvas.xview_滚动(-1*event.delta,'units'))
#允许用户使用shift鼠标/轨迹板水平滚动
第三级页面(框架):
定义初始化(self,*args,**kwargs):
帧._uu初始化(self,*args,**kwargs)
#创建垂直和水平滚动条
self.canvas=canvas(self,background=“#ffffff”,
highlightcolor=“白色”)
self.canvas.pack(side=“left”、fill=“both”、anchor=“center”,
expand=True)
self.vscrollbar=滚动条(self,orient=“vertical”,
command=self.canvas.yview)
self.vscrollbar.pack(side=“right”,fill=“y”)
self.canvas.configure(yscrollcommand=self.vscrollbar.set)
self.hscrollbar=滚动条(self,orient=“horizontal”,
command=self.canvas.xview)
self.hscrollbar.pack(side=“bottom”,fill=“x”)
configure(xscrollcommand=self.hscrollbar.set)
self.container=Frame(self.canvas,highlightcolor=“白色”)
self.canvas.create_窗口(0,0,window=self.container,
anchor=“中心”)
self.container.bind(“,self.onFrameConfigure)
self.canvas.bind_all(“,self.on_垂直)
self.canvas.bind_all(“,self.on_水平)
self.label=label(self.container,text=“Hello World 2.0!”)
self.label.pack(side=“top”,fill=“both”,expand=False)
def onFrameConfigure(自我,事件):
#重置滚动区域以包围内部框架
self.canvas.configure(scrollregion=self.canvas.bbox(“全部”))
def on_垂直(自身、事件):
self.canvas.yview\u滚动(-1*event.delta,“units”)
#允许用户使用鼠标/trac