2016-09-02 117 views
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() 

是否有可能一個接一個地顯示這些文本而不是全部同時顯示?我把time.sleep()證明它沒有單獨出現Python Tkinter文本框插入問題

編輯:這是我的代碼。所以問題是,如果我使用self.tbox.insert(END, "text")而不是print("text"),那麼文本不會以相同的方式出現,如果我使用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() 
+0

您可以逐個插入它們。你在問如何讓他們一個接一個地出現? –

+0

@Bryan Oakley是的,我的意思是,我解決了這個問題。 –

回答

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

+0

實際上,我不想在給定時間插入這些文本,我只是把'time.sleep()'反映出代碼需要大約那段時間來執行代碼。我希望這些文字在出現問題時出現。就像 'if x == y: self.tbox.insert(END,「Some text」)#this is right right now# 問題是程序首先執行命令,然後出現這些文本框。我希望這些文本以精確時刻出現 –

+0

@fedoraman分享您的代碼樣本。我無法理解'這會出現在這一刻',然後'那些文字出現在方框中' –

+0

@ Nehal J Wani我編輯了我的文章並做了更好的解釋 –

0

如果你想使用time.sleep(),你將需要使用Threading模塊來顯示文本逐一。

from tkinter import * 
import time 
import threading 

class Threader(threading.Thread): 

    def __init__(self, tbox, *args, **kwargs): 

     threading.Thread.__init__(self, *args, **kwargs) 
     self.tbox = tbox 
     self.daemon = True # Stop threads when your program exits. 
     self.start() 

    def run(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") 

      break 

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

      button = Button(self.root, text="Button", command= lambda: Threader(tbox=self.tbox)).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() 
MyClass()