Reputation: 106
Tkinter is not displaying the full paragraph in a window
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?
Upvotes: 0
Views: 64
Reputation: 1826
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.
Tcl does not support all the characters. 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)
Upvotes: 1