1

When I try to use scroll on canvas my canvas vanishes and only scroll appears when I uncomment the scroll lines.

from tkinter import *
from utils import editionMap, topicMap, langMap

root = Tk()

''' Top Frame to filter News by Country, Location, Language '''
Top_Frame = Frame(root)

editionMap_dropdown = StringVar()
topicMap_dropdown = StringVar()
langMap_dropdown = StringVar()
Button_Go = StringVar()

editionMap_dropdown.set("Country")
topicMap_dropdown.set("Topic")
langMap_dropdown.set("Language")
Button_GetNews = Button(Top_Frame, text="Get News")


e = OptionMenu(Top_Frame, editionMap_dropdown, *editionMap.keys())
t = OptionMenu(Top_Frame, topicMap_dropdown, *topicMap.keys())
l = OptionMenu(Top_Frame, langMap_dropdown, *langMap.keys())

e.grid(row=0, column=0)
t.grid(row=0, column=1)
l.grid(row=0, column=2)
Button_GetNews.grid(row=0, column=3)


Top_Frame.grid(row=0, column=0)


c = Canvas(root, bg="red", width=900, height=500, scrollregion=(1,0,1000,1000))
#scrollbar = Scrollbar(c)            <---Uncomment
#scrollbar.grid(row=1, column=0)     <---Uncomment

c.grid(row=1, column=0)


root.mainloop()

In right side of canvas there should be a scrollbar in Y-direction. enter image description here

When I uncomment the scrollbar statements I get weird output (see screenshot).

enter image description here

Ice fire
  • 143
  • 3
  • 10
  • You are putting both your canvas and scrollbar at row 1 and column 0, so the scrollbar replaces the canvas. – j_4321 Dec 12 '17 at 11:51
  • @j_4321 If I use `row=1` and `column=1` on scrollbar it still does not work. – Ice fire Dec 12 '17 at 11:53
  • 1
    I had not seen that you use `c` as the scrollbar parent. You are griding the scrollbar on the canvas and it makes the canvas shrink to fit the scrollbar. It is better to use the same parent for both the canvas and the scrollbar and then grid them side by side. – j_4321 Dec 12 '17 at 11:57
  • @j_4321 ok I will try – Ice fire Dec 12 '17 at 12:04
  • @j_4321 Works like a charm. Please add short answer I want to vote :) – Ice fire Dec 12 '17 at 12:18

1 Answers1

2

The problem is that you set the canvas as the scrollbar parent: scrollbar = Scrollbar(c), so when you grid the scrollbar, the canvas shrinks to fit the scrollbar.

So the solution is to use the same parent for both the canvas and the scrollbar and grid them side by side:

c = Canvas(root, bg="red", width=900, height=500, scrollregion=(1,0,1000,1000))
scrollbar = Scrollbar(root)            

c.grid(row=1, column=0)
scrollbar.grid(row=1, column=1, sticky='ns')     
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • Now I am getting problem with the scroll as how can I populate canvas with label and buttons if I use row and column it goes outside canvas. – Ice fire Dec 12 '17 at 13:02
  • 1
    To populate the canvas, the easiest is to do like in the answer to [this question](https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter?noredirect=1&lq=1): add a frame inside the canvas using `c.create_window` and then put all your labels and buttons inside the frame. – j_4321 Dec 12 '17 at 13:08