我如何在tkinter文本窗口中打印出结果?

时间:2019-12-15 17:34:28

标签: python tkinter

如何在tkinter文本窗口中打印出结果?现在,硒的“信息获取”部分正在工作并打印到cmd中,但是TKinter窗口冻结,然后崩溃。我正在考虑为文本窗口更新循环,以便TKinter可以查看是否要进行更新。但是我不确定这是否行得通,因为我相信问题是同时运行代码和窗口。

from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from tkinter import *
import sys

def Program():
#Giving search a value through Entry
    search = entrySearch.get()

#Initializing Webdriver
    driver = webdriver.Chrome('C:/Users/Oliver/Desktop/chromedriver', chrome_options=chrome_options)
    driver.create_options()
    driver.get("http://www.allabolag.se/what/"+search)
    print("Accessing Database...")

#Selecting 'Href' Company Link in search and clicking
    try:
        select = driver.find_element_by_partial_link_text(search).click()
        print("Element Found...")
    except:
        driver.refresh()
        select = driver.find_element_by_xpath('/html/body/div[4]/div[4]/div/div[2]/div[1]/article/div/div/div[1]/h2/a').click()
        print("Backup Element Found...")

#Collecting company information
    try:
        company = driver.find_element_by_xpath('/html/body/div[4]/div[5]/div/div[1]/div[1]/div/div/div/h1').get_attribute("innerHTML")
        revenue = driver.find_element_by_xpath('/html/body/div[4]/div[5]/div/div[1]/div[3]/div/div[1]/div[1]/div[1]/div[2]/div[1]/table/tbody/tr[1]/td[1]').get_attribute("innerHTML")
        profit = driver.find_element_by_xpath('/html/body/div[4]/div[5]/div/div[1]/div[3]/div/div[1]/div[1]/div[1]/div[2]/div[1]/table/tbody/tr[3]/td[1]').get_attribute("innerHTML")
        assets = driver.find_element_by_xpath('/html/body/div[4]/div[5]/div/div[1]/div[3]/div/div[1]/div[1]/div[1]/div[2]/div[1]/table/tbody/tr[4]/td[1]').get_attribute("innerHTML")
        print("Got Results...")
    except:
        company = driver.find_element_by_xpath('/html/body/div[7]/div[5]/div/div[1]/div[1]/div/div/div/h1').get_attribute("innerHTML")
        revenue = driver.find_element_by_xpath('/html/body/div[7]/div[5]/div/div[1]/div[3]/div/div[1]/div[1]/div[1]/div[2]/div[1]/table/tbody/tr[1]/td[1]').get_attribute("innerHTML")
        profit = driver.find_element_by_xpath('/html/body/div[7]/div[5]/div/div[1]/div[3]/div/div[1]/div[1]/div[1]/div[2]/div[1]/table/tbody/tr[3]/td[1]').get_attribute("innerHTML")
        assets = driver.find_element_by_xpath('/html/body/div[7]/div[5]/div/div[1]/div[3]/div/div[1]/div[1]/div[1]/div[2]/div[1]/table/tbody/tr[4]/td[1]').get_attribute("innerHTML")
        print("Got Backup Results...")

        resultOutput.insert(END, company)

#Printing spaces in cmd for easy reading
    i=0
    while i < 25:
        print(" ")
        i+=1

#Printing result
    print("Företag: " +company.strip())
    print("Omsättning: " + revenue.strip() + " 000"+" kr")
    print("Vinst: " + profit.strip() + " 000"+" kr")
    print("Tillgångar: " + assets.strip() + " 000"+" kr")

    restart = input("Tryck 'R' för att start om eller 'S' för att avsluta: ")
    if restart == "s":
        sys.exit()
    else:
        Program()


#Running chromedriver borderless and avoiding annoying log spams
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--log-level=3")

#Initializing TKinter window
root = Tk()

frame = Frame(root, bg='#86c5da')
frame.place(relwidth=1, relheight=1)

entrySearch = Entry(frame, bg='white')
entrySearch.place(relwidth=0.4, relheight=0.05, relx=0.3, rely=0.05)

entryButton = Button(entrySearch, text='Search', bg='#143641', fg='white', command = Program)
entryButton.place(relwidth=0.1, relheight=1, relx=0.9)

resultScroll = Scrollbar(frame)
resultOutput = Text(frame, bg='white', fg='black')
resultScroll.pack(side=RIGHT, fill=Y)
resultOutput.place(relwidth=0.5, relheight=0.6, relx=0.25, rely=0.2)
resultScroll.config(command=resultOutput.yview)
resultOutput.config(yscrollcommand=resultScroll.set)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

使用.after()方法运行代码并与mainloop一起循环。 该方法的很好解释可以在这里找到:https://www.geeksforgeeks.org/python-after-method-in-tkinter/