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)
Related
I have an app that I would like to restyle using Customtkinter. I originally used Tkinter.
However I have a ListBox widget connected to a scrollbar and when I run the code it says that Customtkinter does not have CTkListBox as attribute, or that ListBox is not defined.
I have tried o look for the issue online and it seems that other people have asked to the person who created he module if he could add ListBox widgets (the link below).
https://github.com/TomSchimansky/CustomTkinter/issues/69
He did answer, but as I am new to coding, I do not feel very confident with the code attached.
I hope someone can help.
Thank you
This is my original code:
# create listbox and put it on screen
list1 = Listbox(window, height=6, width=35)
list1.grid(row=2, column=0, rowspan=6, columnspan=2)
#create scrollbar and put it on screen
sb1 = Scrollbar(window)
sb1.grid(row=2, column=2, rowspan=6)
# apply configure methods to make list and scrollbar work together
list1.configure(yscrollcommand = sb1.set)
sb1.configure(command=list1.yview)
# bind method takes two args: type of event and function.
list1.bind("<<ListboxSelect>>", get_selected_row)
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 am having an issue with a TkInter interface whereby I cannot get a scrollbar to visually attach itself to a Listbox element - see this image: Problem interface
Here is the code which creates and positions the Listbox & Scrollbar:
lblpd3 = ttk.Label(mainframe,text='',font=("Helvetica", 5))
lblpd3.grid(column=0, row=12, sticky=NW)
scltrn = Scrollbar(mainframe, orient=VERTICAL)
lbltrn = ttk.Label(mainframe,text='Select Transformation',font=("Helvetica", 11, "bold"))
lbltrn.grid(column=0, row=13, sticky=NW)
self.lsttrn = Listbox(mainframe,selectmode=SINGLE,exportselection=0,width=62,height=4,yscrollcommand=scltrn.set,activestyle='none',selectbackground='#4A6984',selectborderwidth=3,highlightcolor='#4A6984',highlightthickness=1)
scltrn.config(command=self.lsttrn.yview)
scltrn.grid(column=0, row=14, sticky=(N,S,E))
for item in self.coord:
self.lsttrn.insert(END, item)
self.lsttrn.grid(column=0, row=14, padx=0, sticky=NW)
self.lsttrn.select_set(0)
Is there a simple hack I could use to push the scrollbar a few pixels to the left - documentation seems to suggest there's no padding element?
You could add your Scrollbar widget and your Listbox widget into their own seperate Frame.
Then add that in as a single 'unit'.
While I dont know your whole program, here's what the logic behind the fix should roughly look like:
# Declare a new Frame to hold your Listbox and Scroll wheel
myFrame = Frame.__init__(self, parent)
# Make sure your Scrollbar is a part of our newly created Frame "myFrame"
scltrn = Scrollbar(myframe, orient=VERTICAL)
# Make sure your Listbox is a part of our newly created Frame "myFrame"
self.lsttrn = Listbox(myFrame,selectmode=SINGLE,exportselection=0,width=62,height=4,yscrollcommand=scltrn.set,activestyle='none',selectbackground='#4A6984',selectborderwidth=3,highlightcolor='#4A6984',highlightthickness=1)
...
#pack the Scrollbar and Listbox together in our Frame IN THIS ORDER
self.lsttrn.pack()
scltrn.pack()
....
# Now grid our Frame containing both the Scrollwheel and Listbox to your GUI
myFrame.grid(column=xxx,row=xxx,padx=xxx,sticky=xxx)
This shouldnt be a copy+paste fix, but hopefully you understand the logic behind creating a Frame to hold your Scrollbar and Listbox, and then gridding that in, rather than both seperately.
Hope this helps! ~Gunner
At least part of the problem stems from the fact that you are putting the scrollbar and listbox in the same column. They need to be in different columns.
One really simple solution is to use a frame that contains only the listbox and scrollbar. Because you don't have a horizontal scrollbar you can use pack to put the listbox and scrollbar in the frame with just a couple lines of code. You can then place that frame in a single column in its parent.
I'm trying to add a simple log output to a tk window/frame.
So far i only found how to (easily) add a vertical scrollbar on canvas, entry lists and text (which is a fullblown text editor and has no textvariable linking support)
The Label can't be attached to a scrollbar (easily) because it lacks the yview attribute.
#my naive attempt:...
self.lbl_log = tk.Label(self, width=80, height=10, textvariable=self.string_log)
self.lbl_log.pack(side="top")
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.lbl_log.yview)
self.lbl_log.configure(yscrollcommand=self.vsb.set)
AttributeError: 'Label' object has no attribute 'yview'
is there any easy and convenient way to scroll a label widget with several lines in python tk? I don't necessarily have to use a Label i just like it because it is simple and has the textvariable convenience) so i'm open to alternative widgets for this problem.
No, there isn't an easy way to scroll a label. If you need to scroll multiple lines, a label is the wrong choice of widget. If you need to scroll multiple lines of text, the text widget is the proper widget.
I am trying to convert a game I had previously written in command line into GUI (https://github.com/abhinavdhere/Share-Trader-PC/releases/tag/v1.0)
I have built a menu like structure of buttons in one frame, and then on clicking help, the previous frame f1 should disappear and help text should be displayed. I used a Message widget to display the text but it is long and needs scrollbar. I tried to add a vertical scrollbar but couldn't make it work. I referred python and tkinter: using scrollbars on a canvas and tried to do it that way but it still displays only the Message but no scrollbar. Here is the function for it:
def help(self):
self.f1.pack_forget()
f2=tk.Frame(self,bg='#FFCC00')
f2.grid(row=0,column=0)
helpMan=open("Game Rules.txt","r")
hText=helpMan.read()
c1=tk.Canvas(f2,width=640,height=480,scrollregion=(0,0,700,500))
c1.pack(side="left",expand=True,fill="both")
text1=tk.Message(f2,text=hText)
c1.create_window(0,0,anchor="nw",window=text1)
scrollY=tk.Scrollbar(f2,orient="vertical",command=c1.yview)
scrollY.pack(side="right",fill="y")
c1.config(yscrollcommand = scrollY.set)
P.S. Why is it such a hassle to make a simple scrollbar?
The message widget does not support scrolling. It is missing the commands yview and xview that are used for the scrolling protocol. It is really just a multiline label. It is also ugly and can't be themed.
You should replace the message widget with a text widget which also displays multiline text and can support scrolling and formatted text using tags to attach styling information if required.
To make the text widget look the same as the Message widget the following should work:
m = Message(root)
txt = Text(root, background=m.cget("background"), relief="flat",
borderwidth=0, font=m.cget("font"), state="disabled")
m.destroy()