0

I'm making a project where I have to display logs in a frame with the help of Tkinter. Here is my code for that particular frame.

# frame3 for logs
frame3 = Frame(
    win,
    bg='black',
    width=310,
    height=140,
    padx=0,
    pady=0)
frame3.pack(fill=X, expand=True, padx=(0, 10), pady=0)
frame3.pack_propagate(0)  # stops frame from shrinking
scroll = Scrollbar(frame3)
scroll.pack(side = RIGHT, fill = Y)

The logs are generated and are printed in that frame. Here is the code for generating and printing logs

logs = Label(frame3, text = (time.ctime()), font=("Consolas", 9), bg="#000000", fg="#ffffff")
logs.pack(pady=(0, 0))

enter image description here

The scrollbar is showing but it is somehow not working. The scroll is sliding if I click and slide it with the mouse. I guess there are 3 types of scrollbars in Tkinter (Correct me if I'm wrong).

  1. window scrollbar.
  2. Frame scrollbar.
  3. Label Scrollbar (not sure about that).

I think the problem is that I made is a scrollbar for a frame. But, I need it for Label. Or is there any way by which I can print logs directly onto the frame? Don't know what the actual problem is. Also, is there a way by which I can make it auto scrollable when the logs are generated?

Any help would be greatly appreciated. Thanks in advance.

Vraj Kotwala
  • 150
  • 8

2 Answers2

3

Here is an example using tkinter.scrolledtext:

from tkinter import *
from tkinter import scrolledtext

root = Tk()

txt = scrolledtext.ScrolledText(root)
txt['font'] = ('consolas', '12')
txt.pack(expand=True, fill='both')
txt.configure(state=DISABLED)

def log(data):
    txt.configure(state=NORMAL)
    txt.insert(END, data+'\n')
    txt.configure(state=DISABLED)

log('abc')
log('abcde')

root.mainloop()

Hope that's helpful!

rizerphe
  • 1,340
  • 1
  • 14
  • 24
0

.see("end") method helps autoscroll.

Vraj Kotwala
  • 150
  • 8
  • 2
    While this code may solve the OP's issue, it is better to add context, or an explanation as to why this solves the OP's problem, so future visitors can learn from your post, and apply this knowledge to their own issues. High Quality Answers are much more likely to be upvoted, and contribute to the value and quality of SO as a platform. You can also add a link to documentation, if that helps. – SherylHohman May 07 '20 at 17:49