2017-04-05 55 views
0

我正在嘗試使用Tkinter。運行我的程序時,應該出現一個彈出按鈕。按下按鈕後,代碼被執行並且網站被解析。解析本身工作正常,但與Tkinter它沒有。此外,代碼在按下按鈕之前執行。如果有人能指出我犯的錯誤,我將非常感激。Tkinter不運行功能

from lxml import html 
import requests 
from bs4 import BeautifulSoup 

def news(): 
    page = requests.get('http://www.globo.com/index.html') 

    soup = BeautifulSoup(page.content, 'html.parser') 
    bbb = soup.find_all('p', class_='hui-premium__title') 
    for item in bbb: 
     ccc = item.get_text('p') 
     print(ccc) 


from tkinter import * 
master = Tk() 

b = Button(master, text="latest news", command='news()') 
b.pack() 

mainloop() 
+0

不要使用HTML手工格式化代碼。只需粘貼,選擇它,然後單擊「{}」按鈕或按Ctrl + K。這將它縮進4個空格,告訴Stack Overflow將其格式化爲代碼。 – Chris

回答

0

news()Button創建代碼刪除()。您不想在創建按鈕時運行該功能。你想註冊它以備將來使用。

1

command屬性必須被賦予一個可調用的函數,而不是一個字符串。

例如:

b = Button(..., command=news)