简体   繁体   中英

Scrollbar with tkinter messing up

The code below was originally developed to understand how to render a list of urls in a frame and access them by clicking on them. I also had a few buttons to score them. In my project, I have now a long number of urls and need a scrollbar to access them. I have added one, but the urls are not rendered in the part of the frame where the scollbar can be used. In fact, I would like the entire bottomframe to be linked to the scrollbar and used to render the list of urls. I do not understand what I have done wrong. Could anyone help me please?

EDIT: following the answer below, the code below is now working

import tkinter as tk
import webbrowser

class Pierre:
    """This class holds all the objects, data and functions for a single line"""
    def __init__(self, master, url):
        self.url = url
        self.counter = 0
        _, i = master.grid_size() # get the current row number

        lbl = tk.Label(master, text=url, fg="blue", cursor="hand2")
        lbl.grid(row=i, column=0)
        lbl.bind("<Button-1>", self.callback)

        self.DisplayButton = tk.Button(master, text = self.counter)
        self.DisplayButton.grid(row=i, column=1)
        self.DisplayButton.config(height = 1, width = 1 )

        self.Plus1Button = tk.Button(master, text = "+1", command=self.plus1, bg="green")
        self.Plus1Button.grid(row=i, column=2)
        self.Plus1Button.config(height = 1, width = 1 )

        self.Neg1Button = tk.Button(master, text = "-1", command=self.neg1, bg="green")
        self.Neg1Button.grid(row=i, column=3)
        self.Neg1Button.config(height = 1, width = 1 )

        master.update_idletasks()

    def plus1(self):
        self.counter += 1
        self.DisplayButton["text"]=str(self.counter)

    def neg1(self):
        self.counter -= 1
        self.DisplayButton["text"]=str(self.counter)

    def callback(self, event):
        webbrowser.open_new(self.url)

class TestClass(tk.Tk):
    def __init__(self, **kwargs):
        tk.Tk.__init__(self, **kwargs)
        self.title('Test')

        self.topframe = tk.Frame(self)
        self.topframe.pack( side = tk.TOP, pady=30)

        self.bottomframe = tk.Frame(self, width=250, height=190, bg="#EBEBEB")
        self.bottomframe.pack( side = tk.BOTTOM )

        self.canvas = tk.Canvas(self.bottomframe, width=250, height=190,
                                scrollregion=(0, 0, 1200, 800))
        self.canvas.grid()
        self.vscrollbar = tk.Scrollbar(self.bottomframe, orient=tk.VERTICAL,
                                       command=self.canvas.yview)
        self.vscrollbar.grid(row=0, column=5, sticky=tk.N+tk.S)

        self.canvas['yscrollcommand'] = self.vscrollbar.set
        self.frameCanvas=tk.Frame(self.canvas)
        self.canvas.create_window((0,0),window=self.frameCanvas,anchor='nw')

        #self.canvas.configure(scrollregion=self.canvas.bbox("all"))

        self.button = tk.Button(self.topframe, text='Click', command = self.output_value)
        self.button.pack(side="left", fill="both", expand=True)

     ####define the function that the submit button will do
    def output_value(self):
        urls = ["http://www.google.com", "http://www.facebook.com","http://www.google.com", "http://www.facebook.com", "http://www.google.com", "http://www.facebook.com"]
        for url in urls:
            Pierre(self.frameCanvas, url)

if __name__ == "__main__":
    root = TestClass()
    root.mainloop()

Frames do not have scrolling ability. The usual way is to use a canvas and then to place widgets on the canvas with create_window() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM