Python Tkinter文本框插入问题

时间:2016-09-02 13:07:22

标签: python tkinter textbox python-3.5

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()

是否可以逐个显示这些文本而不是同时显示所有文本?我把time.sleep()证明它没有单独出现

编辑:这是我的代码。所以问题在于,如果我使用self.tbox.insert(END, "text")而不是print("text"),那么该文本看起来并不一样,如果我使用print,它当然会立即出现(打印)。我创建了一个网站爬虫或类似的东西,所以等待文本出现在文本框中是非常令人沮丧的。是的,我不想在这种情况下使用print

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 个答案:

答案 0 :(得分:1)

time.sleep()是一个阻止调用。使用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()

演示:

enter image description here

答案 1 :(得分:0)

如果您想使用<script type="text/javascript" src="~/jsNew/bootstrap.min.js"></script> <script type="text/javascript" src="~/jsNew/service-level-meter.js"> </script> <script type="text/javascript" src="~/jsNew/avg-handle-meter.js"></script> <script type="text/javascript" src="~/jsNew/total-calls-meter.js"></script> <script type="text/javascript" src="~/jsNew/avg-speed-meter.js"></script> <script type="text/javascript" src="~/jsNew/abandoned-rate-meter.js"></script> <script type="text/javascript" src="~/js/bootstrap.min.js"></script> 逐个展示文字,则需要使用time.sleep()模块。

Threading