0

I am trying to create a scrollbar that keeps adding another line of text every second. I want it to work "live" and keep adding another line of text.

from tkinter import *
import time

master = Tk()
while True:
    scrollbar = Scrollbar(master)
    scrollbar.pack(side=RIGHT, fill=Y)

    listbox = Listbox(master, yscrollcommand=scrollbar.set)
    listbox.insert(END, "Another line\n")
    listbox.pack(side=LEFT, fill=BOTH)

    scrollbar.config(command=listbox.yview)

    time.sleep(1)
AHsoft
  • 1
  • This code creates an entirely new scrollbar and an entirely new listbox every second. – Bryan Oakley Jan 18 '20 at 20:27
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Jan 18 '20 at 20:55
  • Does this answer your question? [Python Tkinter: Attach scrollbar to listbox as opposed to window](https://stackoverflow.com/questions/24656138/python-tkinter-attach-scrollbar-to-listbox-as-opposed-to-window) – stovfl Jan 18 '20 at 20:56
  • create scrollbar and list only once before `while` loop. You could also use `root.after` instead of `while` and `sleep` to run some code periodically - it will not block `root.mainloop()` which has to work all time. – furas Jan 18 '20 at 21:19

1 Answers1

1

You shouldn't use while True and sleep in any GUI framework (in any language) because it can block mainloop and it can't get events froms system, send them to widgets and redraw window - so window freeze.

Every framework has some method to run code periodically (ie. timer).

tkinter has root.after(milliseconds, function_name) for this

#from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
import time

# --- function ---

def add_line():
    listbox.insert('end', time.strftime("%H:%M:%S"))
    root.after(1000, add_line)  # run `add_line` again after 1000ms (1s)

# --- main ---

root = tk.Tk()

scrollbar = tk.Scrollbar(root)
scrollbar.pack(side='right', fill='y')

listbox = tk.Listbox(root, yscrollcommand=scrollbar.set)
listbox.pack(side='left', fill='both')

scrollbar.config(command=listbox.yview)

add_line()  # run `add_line` first time

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks a lot, how would I be able to add a line after asking for an input? For example python asks you for an input and if your input is something like "yes" it adds another line? Also can I make links within the scrollbar clickable? Thanks a lot. – AHsoft Jan 18 '20 at 22:17
  • if you want to use GUI then learn how to use `Entry` + `Button` instead of `input()`. In `Entry` you can put text and `Button` can run function which will copy text from `Entry` to `Listbox`. Or simple create `Button` with text `Yes`. – furas Jan 18 '20 at 23:29
  • do you means links in text in listbox, not in scrollbar? I'm not sure if you can do it in Listbox but you could try to do it with `tags` in `Text` but if you have link in longer text then you will have to on your own recognize link in this text - and it can be problem. – furas Jan 18 '20 at 23:31
  • effbot.org: [Tkinter Text Widget Hyperlink Manager](http://effbot.org/zone/tkinter-text-hyperlink.htm) – furas Jan 20 '20 at 05:05