Python scrollbar on text widget - python

I'm trying to get a simple scrollbar to show up on the text widget...I've derived the following code which adds the scrollbar but seems to eliminate the text widget completely.
eula = Tkinter.Text(screen1_2)
eula.insert("1.0", "text")
eula.pack(side="left",fill="both",expand=True)
scroll = Tkinter.Scrollbar(eula)
scroll.pack(side="right",fill="y",expand=False)

Note that you must define yscrollcommand=scroll.set in the Text widget, and configure the scrollbar using the line: scroll.config(command=eula.yview)
Here is your code, rewritten with one scrollbar:
# Import Tkinter
from Tkinter import *
# define master
master = Tk()
# Vertical (y) Scroll Bar
scroll = Scrollbar(master)
scroll.pack(side=RIGHT, fill=Y)
# Text Widget
eula = Text(master, wrap=NONE, yscrollcommand=scroll.set)
eula.insert("1.0", "text")
eula.pack(side="left")
# Configure the scrollbars
scroll.config(command=eula.yview)
mainloop()
Here is a Tkinter example with two scrollbars:
# Import Tkinter
from Tkinter import *
# define master
master = Tk()
# Horizontal (x) Scroll bar
xscrollbar = Scrollbar(master, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)
# Vertical (y) Scroll Bar
yscrollbar = Scrollbar(master)
yscrollbar.pack(side=RIGHT, fill=Y)
# Text Widget
text = Text(master, wrap=NONE,
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
text.pack()
# Configure the scrollbars
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
# Run tkinter main loop
mainloop()

Related

How to put Scrollbar inside a Listbox in Tkinter?

how can i move the scrollbar inside a listbox? See the photo attached i used a red arrow for explain, here is my code:
from tkinter import *
window = Tk()
window.geometry('500x500')
window.config(bg='#3c3f41')
window.state('zoomed')
frame_listbox = Frame(window,bg='#3c3f41')
frame_listbox.pack(side=LEFT)
listbox = Listbox(frame_listbox,font=('Helvetica',30))
listbox.pack(padx=400)
scrollbar = Scrollbar(window)
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=listbox.yview)
listbox.config(yscrollcommand=scrollbar.set)
window.mainloop()
I tried to put a scrollbar inside a Listbox, i expect to understand if it's possible or not
It is recommended to put the scrollbar just to the right of the listbox instead of inside it. To achieve it:
make the scrollbar child of frame_listbox instead of window
move option padx=400 from scrollbar.pack(...) to frame_listbox.pack(...)
pack listbox to the LEFT side
from tkinter import *
window = Tk()
window.geometry('500x500')
window.config(bg='#3c3f41')
window.state('zoomed')
frame_listbox = Frame(window,bg='#3c3f41')
frame_listbox.pack(side=LEFT,padx=400) # added padx=400
listbox = Listbox(frame_listbox,font=('Helvetica',30))
listbox.pack(side=LEFT) # removed padx=400 and added side=LEFT
scrollbar = Scrollbar(frame_listbox) # changed parent to frame_listbox
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=listbox.yview)
listbox.config(yscrollcommand=scrollbar.set)
window.mainloop()
Does this will Help? By using grid instead place.
import tkinter as tk
root = tk.Tk()
root.title("Listbox Operations")
# create the listbox (note that size is in characters)
listbox1 = tk.Listbox(root, width=50, height=6)
listbox1.grid(row=0, column=0)
# create a vertical scrollbar to the right of the listbox
yscroll = tk.Scrollbar(command=listbox1.yview, orient=tk.VERTICAL)
yscroll.grid(row=0, column=1, sticky=tk.N+tk.S)
listbox1.configure(yscrollcommand=yscroll.set)
for i in range(1, 50):
listbox1.insert(tk.END, i)
root.mainloop()
Result:

Add a scrollbar in the text that appears after a button

Some outputs create too much text that cannot be viewed without adding a scrollbar in the new window that appears.
For example, in this code below, if there are many files to be returned in the window, you will not be able to view all the text and therefore a scrollbar is needed. How can this be done?
import os
import tkinter as tk
def get_filenames():
filenames = sorted(os.listdir('.'))
text = "\n".join(filenames)
label['text'] = text # change text in label
root = tk.Tk()
# create the text widget
text = tk.Text(root, height=10)
text.grid(row=0, column=0, sticky=tk.EW)
text.grid_columnconfigure(0, weight=1)
text.grid_rowconfigure(0, weight=1)
tk.Button(root, text="OK", command=get_filenames).grid()
# create a scrollbar widget and set its command to the text widget
scrollbar = ttk.Scrollbar(root, orient='vertical', command=text.yview)
scrollbar.grid(row=0, column=1, sticky=tk.NS)
# communicate back to the scrollbar
text['yscrollcommand'] = scrollbar.set
label = tk.Label(root) # empty label
label.grid()
root.mainloop()
How can the scrollbar work as expected?
Scrollbar works correctly but you put text in wrong widget.
def get_filenames():
filenames = sorted(os.listdir('.'))
#text.delete(0, 'end') # remove previous content
text.insert('end', "\n".join(filenames))

Tkinter treeview widgets not properly aligned/added space between widgets

I am working on this table in tkinter made from a bunch of treeveiw widgets. The idea is to get a table where I can add lines, select lines and edit them. In the code below you can add lines to the table by pushing the button. I now want to control the height of each row by configuring the style. However, when I use style the alignment of the treeview widgets is messed up, see attached picture. Any suggestions how to fix this?
EDIT: The problem is the added space between the widgets.
The code for the table is:
from tkinter import *
from tkinter import ttk
class MyApp(Tk):
def __init__(self):
super(MyApp, self).__init__()
self.geometry('950x500+100+100')
self.NewTree = []
label = Label(self,text='Table with some data', font=("Arial Bold", 25))
label.pack()
self.addLine()
master_frame = Frame(self, bd=3, relief=RIDGE)
master_frame.pack(side=BOTTOM)
# Create a frame for the canvas and scrollbar(s).
frame2 = Frame(master_frame)
frame2.pack(side = BOTTOM)
# Add a canvas in that frame.
self.canvas = Canvas(frame2)
self.canvas.grid(row=0, column=0)
# Create a vertical scrollbar linked to the canvas.
vsbar = Scrollbar(frame2, orient=VERTICAL, command=self.canvas.yview)
vsbar.grid(row=0, column=1, sticky=NS)
self.canvas.configure(yscrollcommand=vsbar.set)
# Create a frame on the canvas to contain the buttons.
self.table_frame = Frame(self.canvas)
# Create canvas window to hold the buttons_frame.
self.canvas.create_window((0,0), window=self.table_frame, anchor=NW)
def addLine(self):
#Make button for adding step
bt = Button(self,text='Add Line',command=lambda: self.addLineMethod())
bt.config(width=9, height=1)
bt.pack()
def addLineMethod(self):
lineNumber = int(len(self.NewTree)/5)
for index in range(5):
s = ttk.Style()
s.configure('MyStyle.Treeview', rowheight=25)
self.NewTree.append(ttk.Treeview(self.table_frame, height=1,show="tree",columns=("0"),style='MyStyle.Treeview'))
#Works fine when using this line instead of those above
#self.NewTree.append(ttk.Treeview(self.table_frame, height=1,show="tree",columns=("0")))
self.NewTree[index+5*lineNumber].grid(row=lineNumber, column=index+1)
self.NewTree[index+5*lineNumber]['show'] = ''
item = str(index+5*lineNumber)
self.NewTree[index+5*lineNumber].column("0", width=180, anchor="w")
self.NewTree[index+5*lineNumber].insert("", "end",item,text=item,values=('"Text text text"'))
self.table_frame.update_idletasks() # Needed to make bbox info available.
bbox = self.canvas.bbox(ALL) # Get bounding box of canvas with Buttons.
# Define the scrollable region as entire canvas with only the desired
# number of rows and columns displayed.
self.canvas.configure(scrollregion=bbox, width=925, height=200)
app = MyApp()
app.mainloop()
Her is a picture of the table with some lines.
Put the style configuration in the __init__() function and the effect will go away. I'm not clear as to why this works.
def __init__(self):
...
s = ttk.Style()
s.configure('MyStyle.Treeview', rowheight=20)

how to create vertical and horizontal scrollbar on top-level window python

I am new in Python.
I create vertical and horizontal scroll bar with the help of a post.
When I resize the toplevel window then scrollbar does't move.
How to make scroll bar move with resizing window. Thanks
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import *
root = Tk()
top=Toplevel(root)
text = Text(top)
vs = Scrollbar(top, orient="vertical")
hs = Scrollbar(top, orient="horizontal")
sizegrip = ttk.Sizegrip(root)
# hook up the scrollbars to the text widget
text.configure(yscrollcommand=vs.set, xscrollcommand=hs.set, wrap="none")
vs.configure(command=text.yview)
hs.configure(command=text.xview)
# grid everything on-screen
text.grid(row=0,column=0,sticky="news")
vs.grid(row=0,column=1,sticky="ns")
hs.grid(row=1,column=0,sticky="news")
sizegrip.grid(row=1,column=1,sticky="news")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
def fill():
for n in range(30):
text.insert("end", 'check scroll bar ' * 20, "", "\n")
Button(root, text='press', command=fill).grid(row=0, column=0)
root.mainloop()
You need to tell the Toplevel to adjust the cell in row=0, column=0 when the window is resized:
# grid everything on-screen
top.columnconfigure(0, weight=1)
top.rowconfigure(0, weight=1)

Why does this scrollbar not work after inserting widgets into a listbox?

Basically, I am trying to add a scrollbar to a window containing widgets. I am able to successfully add a scrollbar to a Listbox widget and after inserting stuff, the program works just the way I want it to. But, I face a problem when I place widgets into the Listbox. The scrollbar appears but it seems to be disabled.
from tkinter import *
root = Tk()
root.geometry("640x480")
root.resizable(0,0)
myscrollbar = Scrollbar(root)
myscrollbar.pack(side=RIGHT, fill=Y)
mylist = Listbox(root, width=640, height=480, yscrollcommand=myscrollbar.set)
mylist.pack(fill=BOTH, expand=True)
for x in range(1, 101):
mylist.insert(END, Label(mylist, text="Label: "+str(x)).grid(row=x, column=0))
myscrollbar.config(command = mylist.yview)
root.mainloop()
Any way to fix this code?
from tkinter import *
def myScrollcmd(event):
mycanvas.config(scrollregion=mycanvas.bbox('all'))
root = Tk()
mycanvas = Canvas(root)
mycanvas.pack(fill=BOTH, expand=True)
myFrame = Frame(mycanvas)
mycanvas.create_window((0, 0), window=myFrame, anchor=NW)
myScrollbar = Scrollbar(mycanvas, orient=VERTICAL, command=mycanvas.yview)
myScrollbar.pack(side=RIGHT, fill=Y)
mycanvas.config(yscrollcommand=myScrollbar.set)
mycanvas.bind("<Configure>", myScrollcmd)
for x in range(100):
Label(myFrame, text="Text "+str(x)).pack()
root.mainloop()
This works. However, there is one problem that might not be a major one. The problem is, when my cursor is on the canvas, and I'm moving my mouse wheel, the scrollbar doesn't budge. However the scroll bar moves along with my mouse wheel when my cursor is on top of the scroll bar. I can drag the scroll box to scroll up and down and use the scroll buttons to scroll up and down but the mouse wheel only works when my cursor is hovering over the scrollbar widget.

Categories

Resources