tkinter - Canvas中的滚动条无法正常工作

时间:2016-02-01 22:44:02

标签: python python-3.x tkinter tk

我已经搜索过堆栈溢出但是大多数解决方案似乎对我不起作用或者答案不够清楚我不知道我应该在哪里/怎么做使用代码。 AddSubject框架中的水平滚动条由于某种原因不起作用。如何让水平滚动条工作?

AddSubjects Frame

这是我的代码:

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

class Program(tk.Tk):        
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default = "")
        tk.Tk.wm_title(self, "")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (SubjectHome, AddSubject):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(SubjectHome)



    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()


class SubjectHome(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        ttk.Style().configure("TButton", padding=6, relief="flat", background="#ccc")

        name = tk.Label(self, text = "User: FirstName + LastName")
        name.pack(anchor="ne")

        pagename = tk.Label(self, text = "Subject Menu")
        pagename.pack(anchor="n")

        self.innerFrame = tk.Frame(self, bg="red")
        self.innerFrame.place(relx=.5, rely=.5, anchor="c")        


        view = ttk.Button(self.innerFrame, text = "View Subjects", command = lambda: controller.show_frame(ViewSubject))
        view.grid(row=0, sticky="W"+"E")

        add = ttk.Button(self.innerFrame, text = "Add Subjects", command = lambda: controller.show_frame(AddSubject))
        add.grid(row=1, sticky="W"+"E")


class AddSubject(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        name = tk.Label(self, text = "User: FirstName + LastName")
        name.pack(anchor="ne")

        pagename = tk.Label(self, text = "Add Subjects")
        pagename.pack(anchor="n")

        self.innerFrame = tk.Frame(self)
        self.innerFrame.place(relx=.5, rely=.5, anchor="c")

        canvas = tk.Canvas(self.innerFrame)

        self.firstFrame = tk.Frame(canvas)
        self.firstFrame.pack(anchor="n")

        info = tk.Label(self.innerFrame, text = "Information...\n Information....")
        info.pack()

        for x in range(5):
            pagename = tk.Label(self.firstFrame, text = "Unit Name")
            pagename.grid(row=0, column=x)
            self.text = tks.ScrolledText(self.firstFrame, width=50)
            self.text.grid(row=1, column=x ,sticky="E")

        scrollbar = tk.Scrollbar(self.innerFrame, orient="horizontal", command=canvas.xview)
        canvas.configure(xscrollcommand=scrollbar.set)
        scrollbar.pack(side="bottom", fill="x")
        canvas.pack(side="left", fill="both", expand=True) 

        back = ttk.Button(self.innerFrame, text = "Back", command = lambda: controller.show_frame(SubjectHome))
        back.pack(anchor="sw")

        next = ttk.Button(self.innerFrame, text = "Next")
        next.pack(anchor="se")


app = Program()
app.state('zoomed')
app.mainloop()

1 个答案:

答案 0 :(得分:3)

要在窗体上滚动窗口小部件,必须使用create_window将其添加到画布中。如果您致电packgridplace将小部件放入画布,则不会滚动。