0

I have written the following code to get the user input. But I am not able to add a scrollbar to it. I want to place a vertical scrollbar because I am not able to view all the input labels on my screen. I first tried:

 v = Scrollbar(root, orient='vertical')
 v.config(command=root.yview)

It gave me the following error:

 File "/Users/aaditya/Desktop/Blender_software/Blender_algo_exp/testing.py", line 235, in <module>
    label1.grid(row = 1, column = 0, padx = 10, pady = 10)
  File "/opt/anaconda3/envs/blender_env/lib/python3.9/tkinter/__init__.py", line 2486, in grid_configure
    self.tk.call(
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

After that I tried the following:

  myscroll = Scrollbar(root)
  myscroll.pack(side = RIGHT, fill = Y)

Which resulted in the following error:

AttributeError: '_tkinter.tkapp' object has no attribute 'yview'

How can I fix this?

This is my entire code:

# Driver code
if __name__ == "__main__" :

    root = Tk()
    # v = Scrollbar(root, orient='vertical')
    # v.config(command=root.yview)

    # myscroll = Scrollbar(root)
    # myscroll.pack(side = RIGHT, fill = Y)

    root.configure(background = 'light gray')
    root.geometry("700x700")
    root.title("Blender Software")

    label1 = Label(root, text = "Total Quantity: ",
                   fg = 'black', bg = 'white')
    label2 = Label(root, text = "Percentage of Solid Scrap : ",
                   fg = 'black', bg = 'white')
    label3 = Label(root, text = "Cr min : ",
                   fg = 'black', bg = 'white')
    label4 = Label(root, text = "Cr max : ",
                   fg = 'black', bg = 'white')
 

    label1.grid(row = 1, column = 0, padx = 10, pady = 10)
    label2.grid(row = 2, column = 0, padx = 10, pady = 10)
    label3.grid(row = 3, column = 0, padx = 10, pady = 10)
    label4.grid(row = 4, column = 0, padx = 10, pady = 10)
    

    # Create a entry box
    # for filling or typing the information.
    total_quantity = Entry(root)
    per_solid_scrap = Entry(root)
    Cr_min_input = Entry(root)
    Cr_max_input = Entry(root)
    

    # grid method is used for placing
    # the widgets at respective positions
    # in table like structure .

    total_quantity.grid(row = 1, column = 1, padx = 10, pady = 10)
    per_solid_scrap.grid(row = 2, column = 1, padx = 10, pady = 10)
    Cr_min_input.grid(row = 3, column = 1, padx = 10, pady = 10)
    Cr_max_input.grid(row = 4, column = 1, padx = 10, pady = 10)
    
    button1 = Button(root, text = "Submit", bg = "red",
                     fg = "black", command = calculate_op)
    button1.grid(row = 21, column = 1, pady = 10)

    # Start the GUI
    root.mainloop()
  • what is your ```calculate func``? and normaly you only run your root in your "if __name__" – bangKok Jun 03 '21 at 19:23
  • Calculate function is performing some calculations. I did not think it was relevant to this question so I did not include it –  Jun 03 '21 at 19:25
  • Please provide a runnable [mre]. – martineau Jun 03 '21 at 19:26
  • I tried to add the minimal code. If i go lower than 14 labels then I do not require a scrollbar as it fits entirely on my screen –  Jun 03 '21 at 19:27
  • @DeadLol First of all, for each frame/window you have to pick either `.pack` or `.grid`. You can't use both of them (in a single window/frame). Second of all, `` has no attribute `.yview`. If you want to make it scrollable, look at the `ScrollableFrame` that I created [here](https://stackoverflow.com/a/66215091/11106801) – TheLizzard Jun 03 '21 at 19:30
  • 2
    That first error means exactly what it says: you can't use both `grid` and `pack` for widgets that have same parent (in this case root, referenced as "." in the error) – Bryan Oakley Jun 03 '21 at 19:34
  • Are you aware you can't use a scrollbar directly on a group of widgets in a frame or window? – Bryan Oakley Jun 03 '21 at 19:36
  • you can do it by using `text` widget. (scrollbars). but that means you have to give up your entries. (simple way). – bangKok Jun 03 '21 at 19:37
  • @TheLizzard thank you for your help, I am able to follow your example and understand my mistake. So the correct way would be to create a dummy canvas and add all the labels and the scrollbar to it. Right? –  Jun 03 '21 at 19:38
  • @BryanOakley I am sorry but I was not aware of that. I started using tkinter today and do not know a lot about it. –  Jun 03 '21 at 19:39
  • 1
    @DeadLol Well yes. The problem is that there isn't an easy way of making a group of widgets scrollable so we have to use other methods like using a canvas. Ignoring all of the details, you can use it just like a normal `tkinter.Frame`. You might want to search some basic examples of `tkinter.Frame` uses and you should be able to incorporate `ScrollableFrame` in your code. – TheLizzard Jun 03 '21 at 19:43
  • Why is the scroll widget commented. Are you using it for the program or not – PCM Jun 04 '21 at 04:33

1 Answers1

0

Pack and grid cannot be used at the same time. So since you called the pack for the scrollbar, you cannot manage the following widgets by grid anymore. An easy fix is to place the scrollbar with grid function instead. Apart from that, try using a function or class to make your code less lengthy because now it seems hard to read and purposeless.

hfanatic
  • 149
  • 1
  • 4