editor = Text(compiler, height=100, width=100)
editor.pack(side=LEFT,fill=BOTH)
scrollbar = Scrollbar(compiler)
scrollbar.pack(side=RIGHT)
scrollbar.config(command=editor.yview)
Above picture is taken after clicking top scrollbar button once it is scrolling whole textView at once
Now, the problem is I want to build a scrollbar like PyCharm.
scrollbar.pack(side=RIGHT,fill=Y)
When I tried above code scrollbar was looking like this
scrollbar is unable to scroll properly(it is scrolling whole at once by clicking scroll button and when I am using fill=Y it is giving me huge scrollbar while I don't have anything in my textView). And, I want to scrollbar to works like PyCharm's scrollbar.
Thanks to #acw1668 and #TheLizzard
editor = Text(compiler, height=100, width=100)
scrollbar = Scrollbar(compiler)
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=editor.yview)
editor.config(yscrollcommand=scrollbar.set)
editor.pack(side=LEFT,fill=BOTH)
It's working....now...
Related
Im coding my personal text editor. But i have a problem with the 2 widget text and the scrollbar (connect one scrollbar to two text).
What is my idea and logic (at the beginning)?
I want to display 2 text, one for writing text entered by user, and one to display the number of the line. I pack both of them, in the root. Then i create a scrollbar, that will scroll on Y axes the 2 text, so what i want to do (mainly) is to connect 2 widget (text) to one scrollbar.
But it didn't work.
This system absolutely doesn't work, are there any suggest or fix to fix this first idea?
Other ideas that i found.
After the first attempt, i thought that i can pack the 2 texts into 1 container. I tried to create a frame (packed into root) that contains the 2 texts, i did this because i have to connect the scrollbar only to the frame. But it didn't work, moreover it didnt allow me to write the following snippet: command=frame.yview in the scrollbar option, it seems that i cant connect frame to scrollbar.
So:
I will ask u if my reasoning are good, and how to solve. If not what can i do?
Similar question found on Google: (but that i dont undestand)
How to scroll two parallel text widgets with one scrollbar?
Tkinter adding line number to text widget
from tkinter import *
root = Tk()
root.geometry("480x540+100+100")
root.config(cursor='')
line = Text(root, bg="light grey", font="Roman 24", width=4)
line.pack(side=LEFT, fill=BOTH)
text = Text(root, bg="grey", font="Roman 24")
text.pack(side=LEFT, fill=BOTH, expand=True)
scrollbar = Scrollbar(text, orient=VERTICAL, command=(line.yview, text.yview))
text.configure(yscrollcommand=scrollbar.set)
line.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side=RIGHT, fill=Y)
for n in range(50):
line.insert("{}.0".format(n+1), "{}\n".format(n+1))
text.insert("{}.0".format(n+1), "Line no. {}\n".format(n+1))
if __name__ == '__main__':
root.mainloop()
There's nothing special about a scrollbar - it just calls a function when you move it. The API for this function is well defined. While it normally should call the yview (or xview) method of a scrollable window, there's no requirement that it must.
If you want to control two widgets with a single scrollbar, create a function for your scrollbar that scrolls both windows.
def multiple_yview(*args):
line.yview(*args)
text.yview(*args)
scrollbar = Scrollbar(text, orient=VERTICAL, command=multiple_yview)
You will have a similar problem when you scroll the text widget while entering new lines or moving around with cursor keys. You'll need to configure the yscrollcommand attribute of the text widget to call a function so that it both updates the scrollbar and also scrolls the other window (and maybe also add additional line numbers)
Apologies for the dumb question, as you can probably tell, I am extremely new to Python. I am trying to attach a scroll bar to a list in a frame, I can scroll up/down the list, but I can only attach it to the frame for some reason. When I try to attach it to listbox, the listbox disappears.
Here is the code:
wipLotListBox = Listbox(tab_printFGWO)
wipLotListBox.insert(1,"Test1")
wipLotListBox.insert(2,"Test2")
wipLotListBox.insert(3,"Test3")
wipLotListBox.insert(4,"Test4")
wipLotListBox.insert(5,"Test5")
wipLotListBox.insert(6,"Test6")
scrollBar = ttk.Scrollbar(tab_printFGWO,orient=VERTICAL,command=wipLotListBox.yview)
scrollBar.place(x=700,y=365)
wipLotListBox.config(yscrollcommand=scrollBar.set)
wipLotListBox.place(x=700,y=365)
Here is what it looks like:
place is almost always the wrong choice. Both pack and grid are much better.
Assuming that the scrollbar and listbox are the only two widgets in tab_printFGWO, here's the easiest way to do it:
scrollBar.pack(side="right", fill="y")
wipLotListBox.pack(side="left", fill="both", expand=True)
I know this isn't the first time a question like this is asked, but even after like 2 hours of browsing the Internet I can't get it to work:
So I'm trying to create a Tkinter-Frame, that contains several Buttons (As Example I took 30). But Because I don't have enough space in my program, I need to add an Scrollbar next to it, so that one can scroll through the Buttons.
The Problems I had where, that the inner "moving part" of the bar was as big as the whole scrollbar and couldn't be moved, which I kinda solved by using scollregion=(0,0,1000,1000), but even then the moving of the bar had no effect on the canvas whatsoever.
Here Is the corresponding code that I extracted out of my program:
import Tkinter as tk
root = tk.Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=50)
root.columnconfigure(1, weight=1)
root.minsize(300,400)
root.maxsize(300,400)
#Buttons
buttonFrame = tk.Canvas(root, bg='#bbb')
buttonFrame.grid(row=0, column=0, sticky=tk.N+tk.E+tk.S+tk.W)
buttonFrame.columnconfigure(0, weight=1)
scroll = tk.Scrollbar(root, command=buttonFrame.yview)
scroll.grid(row=0, column=1, sticky=tk.N+tk.E+tk.S+tk.W)
buttonFrame.configure(yscrollcommand=scroll.set)
for i in range(30):
tk.Button(buttonFrame, text=str(i+1)).grid(row=i, column=0, sticky=tk.N+tk.E+tk.S+tk.W)
root.mainloop()
As you (hopefully) see, the slider can't even be moved nor does it change anything on the canvas, even if I squeeze a scrollregion=(bla) somewhere in there.
2 Questions:
a.) What do I need to add (or remove), so that I can scroll through the list of Buttons
b.) Does the fix from a. still work when I make the Scrollbar a child of the buttonFrame instead of the root?
To add widgets to a Canvas you have to use the create_window method, not grid(). Then you have to update the canvas before setting the scrollregion.
for i in range(30):
btn = tk.Button(buttonFrame, text=str(i+1))
buttonFrame.create_window((100,i*50), window=btn)
root.update()
buttonFrame.config(scrollregion=buttonFrame.bbox("all"))
If you try that I suspect it's not what you were looking for, since the create_window method requires absolute positioning (you can't use grid or pack). That's why most people put a Frame in the Canvas, and add their widgets to that instead. Many people have abstracted this faux Frame that is actually a Frame in a Canvas in another Frame, including me.
I'm relatively new to Python and coding in general, so I'm a bit rubbish with most of the terminology.
I've researched this myself but I'm still relatively unsure (kept messing up), so hope someone can help me.
I'm looking to attach the scrollbar within my code to the right side of the text widget, rather than having it floating around the bottom corner.
My project is a text adventure (learning Python along to a book), and I have been building a very basic GUI to house the game. Essentially the text box here is placeholder, and it'll be replaced with the game code down the line, with graphics making up the rest of the window surrounding the center text/command prompt. I'm assuming doing this would be doable relatively easily?
I'm happy to work out just how to sort the scrollbar currently, as it's helping me learn!
Thanks guys!
from tkinter import *
root = Tk()
root.title("GAME TITLE")
mainframe = Frame(root, width=720, height=540)
mainframe.pack()
gametext = Text(root, width=75, height=15)
gametext.place(relx=0.5, rely=0.7, anchor=CENTER)
scrollbar = Scrollbar(root, command=gametext.yview)
gametext.config(yscrollcommand=scrollbar.set)
scrollbar.pack(side=RIGHT, fill=Y)
gametext.tag_configure('fonttype1', font = ('Arial', '12', 'bold'))
quote = """GAMEPLAY TEXT HERE"""
gametext.insert(END, quote, 'fonttype1')
root.mainloop()
Sample Screenshot of code running
If all you're using is a text widget and scrollbar, use pack for both. place is best used very, very rarely.
scrollbar.pack(side=RIGHT, fill=Y)
gametext.pack(side=LEFT, fill=BOTH, expand=True)
Hi I can't seem to find the cause of a weird issue I am having in Tkinter\Python
When using the following code to create a scrolling window on a canvas
myFrame = Frame(ob)
ob.create_window((0, 0), window=myFrame, anchor='nw')
scroll = Scrollbar(sub, orient="vertical", command=ob.yview)
ob.configure(yscrollcommand=scroll.set)
scroll.grid(row=0, column=1, sticky=N+S)
# add check boxes for clients to buttons
for row in clients:
v = IntVar()
item = Checkbutton(myFrame, text=row[1], variable=v)
item.va = v
item.grid(sticky='w')
clientlist[str(row[0])] = v
ob.configure(scrollregion=ob.bbox('all'))
It creates the windows inside of the canvas and also creates the items inside of it. It even creates the scrollbar next to the canvas just fine.
The issue is that when I scroll down it allows me to scroll forever. It also scrolls upwards just past the top most item in the canvas window and then disables the scroll function.
Any insight on this as it seems I must not be setting the scroll region correctly is my guess but I am not sure what is wrong with it.