When I watched ecnerwala videos on competitive programming, I found something interesting in his video.
This running text ecnerwala running text
Here is the link of the video https://www.youtube.com/watch?v=ExmrrXi04vI&t=1030s
So I tried to create something like that running text using tkinter.text widget in python. I create a window, add text object to it, and bind all keyboard keys to the window. I insert those keys into the text object when any key is pressed. I also make sure that even if I press the enter button, I don't insert it as a newline to the text widget but as the word 'Return' instead
Of course, there is a possibility that the length of all characters I insert into the text is longer than the length of the text widget could fit in one sentence. That's why I want to make the text widget to "always" see the characters from index END-(the number of character the widget could fit) until END
Here is my code
from tkinter import *
def handle(event):
text.config(state = NORMAL)
text.insert(END, event.keysym)
text.config(state = DISABLED)
# Things to be fix
text.doSomethingToMakeItBehaveLikeEntryWidget()
window = Tk()
window.geometry('500x500')
text = Text(window, font = ('Arial', 20), height = 1)
entry = Entry(window, font = ('Arial', 20))
text.config(state = DISABLED)
text.pack()
entry.pack()
window.bind('<Key>', handle)
window.mainloop()
Is there any solution for this problem? Even if I have to use another widget instead of text widget to solve it? Thanks for the help
CodePudding user response:
Given that you use only one line in the text widget, you can use an entry in read-only mode instead.
No matter whether you use an entry or a text widget, you can use the method text.xview_moveto(<fraction>)
to set which part of the content is visible, <fraction>
being the relative portion of the widget's content which is hidden on the left. If you set this fraction to 1, it will ensure that the last character is visible.
from tkinter import *
def handle(event):
text.config(state=NORMAL)
text.insert(END, event.keysym)
text.config(state="readonly")
text.xview_moveto(1)
window = Tk()
window.geometry('500x500')
text = Entry(window, font = ('Arial', 20), state="readonly", readonlybackground="white")
entry = Entry(window, font = ('Arial', 20))
text.config(state = DISABLED)
text.pack()
entry.pack()
window.bind('<Key>', handle)
window.mainloop()
CodePudding user response:
Try this.
from tkinter import *
from tkinter.scrolledtext import ScrolledText
def handle(event):
text.config(state = NORMAL)
text.insert(END, event.keysym)
text.config(state = DISABLED)
t = text.get('1.0','end-1c')
length= len(t)
width =text.winfo_width()
max_char = int((35/500)*width)
if max_char < length:
a = length-max_char
text.config(state = NORMAL)
text.delete(1.0,END)
text.insert(1.0,t[a:])
text.config(state = DISABLED)
window = Tk()
window.geometry('500x500')
text = Text(window, font = ('Arial', 20), height = 1)
entry = Entry(window, font = ('Arial', 20))
text.config(state = DISABLED)
text.pack()
entry.pack()
window.bind('<Key>', handle)
window.mainloop()