【问题标题】:Python Tkinter textbox inserting issuePython Tkinter 文本框插入问题
【发布时间】:2017-01-10 14:50:40
【问题描述】:
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"),那么文本的显示方式就不一样了,如果我使用打印,它当然会立即出现(打印)。我做了一个网站爬虫或类似的东西,所以当文本出现在文本框中时等待是非常令人沮丧的。是的,我不想在这种情况下使用打印

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

【问题讨论】:

  • 正在将它们一一插入。你是在问如何让它们一个一个出现
  • @Bryan Oakley 是的,我的意思是,我已经解决了。

标签: python tkinter textbox python-3.5


【解决方案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()

演示:

【讨论】:

  • 实际上,我不想在给定的时间内插入这些文本,我只是输入time.sleep() 以反映有代码需要大约那个时间来执行代码。我希望在发生某些事情时出现这些文本。像if x == y: self.tbox.insert(END, "Some text") #this will appear right this moment 问题是程序首先执行命令,然后在框中出现这些文本。我希望这些文字出现在准确的时刻
  • @fedoraman 分享您的代码示例。我无法理解“这会立即出现”和“然后这些文本出现在框中”
  • @Nehal J Wani 我编辑了我的帖子并做出了更好的解释
猜你喜欢
  • 1970-01-01
  • 2012-08-29
  • 2015-11-22
  • 2017-11-06
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
相关资源
最近更新 更多