1
from tkinter import *
import time

class MyClass(object):
    def __init__(self):
        root = Tk()

        button = Button(root, text="Button", command=self.command).pack()

        #scrollbar and textbox
        scrollbar = Scrollbar(root)
        scrollbar.pack(side=RIGHT, fill=Y)

        self.tbox = Text(root, wrap=WORD, yscrollcommand=scrollbar.set)
        self.tbox.pack(fill=X)
        scrollbar.configure(command=self.tbox.yview)

        root.mainloop()
    def command(self):
        time.sleep(2)
        self.tbox.insert(END, "Some text1\n")

        time.sleep(2)
        self.tbox.insert(END, "Some text2\n")

        time.sleep(2)
        self.tbox.insert(END, "Some text3")
MyClass()

Is it possible to appear those texts one by one and not all at the same time? I put time.sleep() to prove that its not appearing those separately

EDIT: Here is my code. So the problem is that if I use self.tbox.insert(END, "text") instead of print("text"), that text is not appearing the same way, if I use print, it will appear (prints) instantly of course. I made a website crawler or something like that, so it is very frustrating to wait when the text appears in textbox. And yes, I dont want to use print in this case

from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from tkinter import *

phantom_path = r'phantomjs.exe'
driver = webdriver.PhantomJS(phantom_path)

class Crawler(object):
    def __init__(self):

        self.root = Tk()
        self.root.title('Website Crawler')
        label1 = Label(self.root, text='Select a website').pack()

        self.website = StringVar()
        Entry(self.root, textvariable=self.website).pack()

        #button which executes the function
        button = Button(self.root, text='Crawl', command=self.command)
        button.pack()

        #scrollbar and textbox
        self.scrollbar = Scrollbar(self.root)
        self.scrollbar.pack(side=RIGHT, fill=Y)

        self.tbox = Text(self.root, wrap=WORD, yscrollcommand=self.scrollbar.set)
        self.tbox.pack(fill=X)
        self.scrollbar.configure(command=self.tbox.yview)

        self.root.mainloop()

    def command(self):

        url = self.website.get()
        link_list = []
        link_list2 = []

        driver.get(url)

        driver.implicitly_wait(5)

        self.tbox.insert(END, "Crawling links..\n")

        #finds all links on the site and appens them to list
        try:
            links = driver.find_elements_by_tag_name('a')
            for x in links:
                x = x.get_attribute('href')
                link_list.append(x)
                self.tbox.insert(END, str(x)+'\n')

        except NoSuchElementException:
            self.tbox.insert(END, 'This site have no links\n')
            pass
        try:
            for sites in link_list:
                driver.get(sites)
                self.tbox.insert(END, "### In "+str(sites)+': ###\n')
                links = driver.find_elements_by_tag_name('a')
                for y in links:
                    y = y.get_attribute('href')
                    link_list.append(y)
                    self.tbox.insert(END, str(y)+'\n')
        except NoSuchElementException:
            self.tbox.insert(END, 'This site have no links\n')
            pass
        self.tbox.insert(END, 'Done\n\n')

Crawler()
2
  • You are inserting them one by one. Are you asking how to make them appear one by one? Commented Sep 2, 2016 at 13:57
  • @Bryan Oakley Yes that what i meant, I fixed that. Commented Sep 2, 2016 at 15:36

1 Answer 1

2

time.sleep() is a blocking call. Use after.

from tkinter import *
import time

class MyClass(object):
    def __init__(self):
        self.root = Tk()

        button = Button(self.root, text="Button", command=self.command).pack()

        #scrollbar and textbox
        scrollbar = Scrollbar(self.root)
        scrollbar.pack(side=RIGHT, fill=Y)

        self.tbox = Text(self.root, wrap=WORD, yscrollcommand=scrollbar.set)
        self.tbox.pack(fill=X)
        scrollbar.configure(command=self.tbox.yview)

        self.root.mainloop()
    def command(self):
        self.root.after(1000, lambda: self.tbox.insert(END, "Some text1\n"))

        self.root.after(2000, lambda: self.tbox.insert(END, "Some text2\n"))

        self.root.after(3000, lambda: self.tbox.insert(END, "Some text3"))
MyClass()

Demo:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, I dont want to insert those texts in given time, i just put time.sleep() to reflect that there is code which takes about that time to execute the code. I want those text to appear when something happens. like if x == y: self.tbox.insert(END, "Some text") #this will appear right this moment the problem is that the program first executes the command and then appears those texts in the box. I want those text appear in exact moment
@fedoraman Share a sample of your code. I am not able to understand 'this will appear right this moment' and 'then appears those texts in the box'
@ Nehal J Wani I edited my post and made better explanation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.