0

I have the following piece of code which lists some urls when you click a button. I would like to add scrollbars to the bottom frame where the urls are listed. Unfortunately,my current attempts leads to "cannot use geometry manager grid inside .!frame2 which already has slaves managed by pack".

I can see that I am mixing grid and pack, but I have not managed so far to solve this. Could anyone help me, please?

import random
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=1)
        lbl.bind("<Button-1>", self.callback)

        self.DisplayButton = tk.Button(master, text = self.counter)
        self.DisplayButton.grid(row=i, column=2)
        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=3)
        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=4)
        self.Neg1Button.config(height = 1, width = 1 )

    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)
        self.bottomframe.pack( side = tk.BOTTOM )

        self.canvas=tk.Canvas(self.bottomframe)
        self.hbar=tk.Scrollbar(self.bottomframe,orient=tk.HORIZONTAL)
        self.hbar.pack(side=tk.BOTTOM,fill=tk.X)
        self.hbar.config(command=self.canvas.xview)
        self.vbar=tk.Scrollbar(self.bottomframe,orient=tk.VERTICAL)
        self.vbar.pack(side=tk.RIGHT,fill=tk.Y)
        self.vbar.config(command=self.canvas.yview)
        self.canvas.config(width=300,height=300)
        self.canvas.config(xscrollcommand=self.hbar.set, 
    yscrollcommand=self.vbar.set)
        self.canvas.pack(side=tk.LEFT,expand=True,fill=tk.BOTH)


        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"]
        for url in urls:
            Pierre(self.bottomframe, url)

if __name__ == "__main__":
    root = TestClass()
    root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
Pierre
  • 69
  • 8

1 Answers1

0

As the error you get indicates, you should not use both pack and grid in the same parent. You should read this post gives you more insight into what you are doing when you use both pack and grid, and also gives you an alternative: conceptual dividing your interface into segments.

Samuel Kazeem
  • 787
  • 1
  • 8
  • 15