(Python)使用滚动条将框架插入到画布中

时间:2017-07-17 10:51:05

标签: python-3.x tkinter tkinter-canvas

我正在尝试取一个框架并将其插入到具有滚动条的画布中,因为数据并不完全适合它自己的窗口。这是我编写框架的代码,但不知道如何将其放入画布并正确编写滚动条以放置在窗口的右侧:

    class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        b = tk.Button(self, text="Done!", command=self.upload_cor)
        b.grid()
        table = tk.Frame(self, width=300, height=300)
        table.grid(row=0,column=0)

        canvas=Canvas(table,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
        vbar=Scrollbar(table,orient=VERTICAL)
        vbar.pack(side=RIGHT,fill=Y)
        vbar.config(command=canvas.yview)
        canvas.config(width=300,height=300)
        canvas.config(yscrollcommand=vbar.set)
        canvas.pack(side=LEFT,expand=True,fill=BOTH)

        data = (word_freq)

        self.widgets = {}
        row = 0
        for word, freq in (data):
            row += 1
            self.widgets[word] = {
                "Word": tk.Label(table, text=word),
                "Freq": tk.Label(table, text=freq),
                "checkbox_var": tk.IntVar(),
                "checkbox": tk.Checkbutton(table)
            }

            self.widgets[word]["Word"].grid(row=row, column=1, sticky="nsew")
            self.widgets[word]["Freq"].grid(row=row, column=2, sticky="nsew")

            self.widgets[word]["checkbox"].config(variable=(self.widgets[word]["checkbox_var"]))
            self.widgets[word]["checkbox"].grid(row=row, column=7, sticky="nsew")

        table.grid_columnconfigure(1, weight=1)
        table.grid_columnconfigure(2, weight=1)
        # invisible row after last row gets all extra space
        table.grid_rowconfigure(row+1, weight=1)

    def upload_cor(self):
        for word in sorted(self.widgets.keys()):
            entry_widget = self.widgets[word]["num_seconds_correction"]
            new_value = entry_widget.get()
            print("%s: %s" % (word, new_value))

        for word in sorted(self.widgets.keys()):
            check_var = self.widgets[word]["checkbox_var"]
            print(check_var.get())
            if check_var.get():
               print('Check button checked')

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).grid(fill="both", expand=True)
    root.mainloop()

2 个答案:

答案 0 :(得分:0)

以下演示了框架内画布和滚动条的网格化。画布包含表格框架,而桌面框架又包含其他网格小部件。

class Example(tk.Frame):
def __init__(self, parent):
    tk.Frame.__init__(self, parent)
    b = tk.Button(self, text="Done!", command=self.upload_cor)
    b.grid()

    canvas=Canvas(self,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
    vbar=Scrollbar(self,orient=VERTICAL)
    vbar.grid(row=1,column=1,sticky='nsw')
    vbar.config(command=canvas.yview)
    canvas.config(width=300,height=300)
    canvas.config(yscrollcommand=vbar.set)
    canvas.grid(row=1,column=0,sticky='news')
    table = tk.Frame(canvas, width=300, height=300)
    canvas.create_window(0,0,anchor='nw',height=300,width=300,window=table)

    data = (word_freq)

    self.widgets = {}
    row = 0
    for word, freq in (data):
        row += 1
        self.widgets[word] = {
            "Word": tk.Label(table, text=word),
            "Freq": tk.Label(table, text=freq),
            "checkbox_var": tk.IntVar(),
            "checkbox": tk.Checkbutton(table)
        }

        self.widgets[word]["Word"].grid(row=row, column=1, sticky="nsew")
        self.widgets[word]["Freq"].grid(row=row, column=2, sticky="nsew")

        self.widgets[word]["checkbox"].config(variable=(self.widgets[word]["checkbox_var"]))
        self.widgets[word]["checkbox"].grid(row=row, column=7, sticky="nsew")

    table.grid_columnconfigure(1, weight=1)
    table.grid_columnconfigure(2, weight=1)
    # invisible row after last row gets all extra space
    table.grid_rowconfigure(row+1, weight=1)

def upload_cor(self):
    for word in sorted(self.widgets.keys()):
        entry_widget = self.widgets[word]["num_seconds_correction"]
        new_value = entry_widget.get()
        print("%s: %s" % (word, new_value))

    for word in sorted(self.widgets.keys()):
        check_var = self.widgets[word]["checkbox_var"]
        print(check_var.get())
        if check_var.get():
           print('Check button checked')

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).grid(sticky='news')
    root.mainloop()

答案 1 :(得分:0)

from tkinter import *
pj = Tk()
pj.geometry("500x600")
fin = Frame(pj, width=200, height=200 )
fin.pack()
fin.place(x=90, y  = 50)`    
canvas=Canvas(fin,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
vbar=Scrollbar(fin,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)