Reputation: 3
I am working on a GUI where pressing a certain button produces a pop-up window that should display a long, scrollable list of text. I'm using the text widget within the popup window to display this text. I've added a scrollbar and it appears on the pop-up window just fine. However when you try to scroll, moving the slider on the scrollbar does nothing. The slider doesn't even move. But the text list will respond and scroll when I use the mouse-wheel. Please see the code below for the command "bib" that is assigned to my button:
def bib():
root = tk.Tk()
root.title("References")
h=tk.Scrollbar(root)
h.pack(side=tk.RIGHT,fill=tk.Y)
v=tk.Scrollbar(root)
t=tk.Text(root,width=90, wrap=tk.NONE, yscrollcommand=v.set)
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.pack(side=tk.TOP, fill=tk.X)
t.configure(font=("Times New Roman", 10))
v.config(command=t.yview)
t.mainloop()
Upvotes: 0
Views: 1347
Reputation: 142641
You create two Scrollbar
- h
and v
- and you display first scrollbar h
but you assign Text
to second scrollbar v
which you don't display.
You have to assign correct Scrollbar
to Text
import tkinter as tk
root = tk.Tk()
root.title("References")
# rename `h` to `v`
v = tk.Scrollbar(root)
v.pack(side=tk.RIGHT, fill=tk.Y)
# rename `v` to `h`
#h = tk.Scrollbar(root)
t = tk.Text(root,width=90, wrap=tk.NONE, yscrollcommand=v.set)
for _ in range(20):
t.insert(tk.END,"number 1\n")
t.insert(tk.END,"number 2\n")
t.pack(side=tk.TOP, fill=tk.X)
t.configure(font=("Times New Roman", 10))
v.config(command=t.yview)
t.mainloop()
Upvotes: 0
Reputation: 385960
Scrollbars and their associated widgets require two-way communication. You've created a one-way communication from the text widget to the scrollbar but not the other way around.
The command for the horizontal scrollbar needs to call the xview
method of the scrollable widget, and the command for the vertical scrollbar needs to call the yview
method.
Upvotes: 1