简体   繁体   English

Tkinter 只显示段落的最后三行

[英]Tkinter only displays last three lines of the paragraph

Tkinter is not displaying the full paragraph in a window Tkinter 未在窗口中显示完整段落

My code:-我的代码:-

import bs4 as bs
import urllib.request
import tkinter as tk  


dinosaur = 'dinosaur'

url = ('https://html.duckduckgo.com/html?q='+dinosaur)

sauce = urllib.request.urlopen(url).read()
soup = bs.BeautifulSoup(sauce, 'lxml')
a = soup.body.b
print(a)

for i in soup.find_all('a', class_='result__snippet'):
    print(i.get_text(separator=' - ', strip=True))
    abc = (i.get_text(separator=' - ', strip=True))



    
    
root = tk.Tk()
S = tk.Scrollbar(root)
T = tk.Text(root, height=4, width=50)
S.pack(side=tk.RIGHT, fill=tk.Y)
T.pack(side=tk.LEFT, fill=tk.Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
T.insert(tk.END, abc)
tk.mainloop()

But it displays the whole paragraph in terminal though但它虽然在终端中显示了整个段落

can anyone point out the mistake i am making?谁能指出我犯的错误?

The following part of the code should be as follows:代码的以下部分应如下所示:

abc=""
for i in soup.find_all('a', class_='result__snippet'):
    print(i.get_text(separator=' - ', strip=True))
    abc += (i.get_text(separator=' - ', strip=True))

The += operator concatenates the string so that you have the full text, and not only the last iteration of the loop. +=运算符连接字符串,以便您拥有全文,而不仅仅是循环的最后一次迭代。

Note笔记

Tcl does not support all the characters. Tcl 不支持所有字符。 Therefore, depending on the character, it would not be possible to be printed.因此,根据字符,将无法打印。

To avoid this problem, add the following lines:为避免此问题,请添加以下行:

abc_clean = ""
char_list = [abc[j] for j in range(len(abc)) if ord(abc[j]) in range(65536)]
for j in char_list:
    abc_clean+=j

And then:进而:

T.insert(tk.END, abc_clean)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM