[英]How to run a python function in a tkinter window?
I made some functions which grab information from websites like weather or news using Python and BeautifulSoup.我使用 Python 和 BeautifulSoup 制作了一些从天气或新闻等网站获取信息的功能。 Basically web scraping.
基本上是web刮。 Now I would like to run them in a different window with Tkinter.
现在我想在不同的 window 和 Tkinter 中运行它们。 How could I implement the weather function, as an example, in my code in order to have it shown not just in the terminal of my text editor but in this Tkinter window?
例如,我如何在我的代码中实现天气 function,以便不仅在我的文本编辑器的终端中,而且在这个 Tkinter window 中显示它?
That's my code for the Tkinter window:这是我的 Tkinter window 的代码:
from tkinter import *
from PIL import ImageTk, Image
from weather import weather_def # that's the function I made in a different file
# main
window = Tk()
window.title('My Overview')
window.configure(background='white')
# create a entry text box
textentry = Entry(window, width=15, bg='white')
textentry.place(x=80, y=40)
# create label
label = Label(window, text='Empty label', bg='white', fg='black', font='none 12 bold')
label.place(x=80, y=120)
# add a submit button, command=click, width=10
btn = Button(window, text='Show information', command=weather_def)
btn.place(x=80, y=80)
# run the main loop
window.mainloop()
And that's the function which grabs information about the weather from a website:这就是从网站获取天气信息的 function:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
url_weather = 'https://www.mein-wetter.com/wetter/ebermannstadt.htm'
# opening up connection, grabbing the page
uClient_weather = uReq(url_weather)
page_html_weather = uClient_weather.read()
uClient_weather.close()
# html parsing
page_soup_weather = soup(page_html_weather, "html.parser")
# grabs containers
containers_weather = page_soup_weather.find('ul', {'class': 'uldet'}).findAll('li')
# grabs time, temperature, text
def weather_def():
print('Wetter')
print('::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::')
for container in containers_weather:
time = container.findAll('h4')[0].text
temperature = container.findAll('dd', {'class': 'temp'})[0].text
image_alt = container.find('img').get('alt')
# only show weather from 6:00 to 23:00
if int(time) == 00:
break
elif int(time[1]) >= 6 or int(time[0]) > 0:
print(time + ':00 Uhr')
print(temperature)
print(image_alt)
print('-----------------------------------------')
Hi @jumuell and welcome to Stack Overflow.嗨@jumuell,欢迎来到 Stack Overflow。 I have seen the comments, but I am answering your question because I found a problem that the others did not.
我已经看到了评论,但是我正在回答您的问题,因为我发现了其他人没有的问题。
For your problem, you need to show multiple lines of text as a result.对于您的问题,您需要显示多行文本作为结果。 The
Text
widget allows multiple lines to be typed in it. Text
小部件允许在其中键入多行。 But the logic part and the GUI part are in separate files.但是逻辑部分和 GUI 部分在不同的文件中。
Create a new Text widget in the GUI file.在 GUI 文件中创建一个新的 Text 小部件。
name_of_text_widget = Text(window)
name_of_text_widget.pack
(You can change pack
to grid
or place
as per your wish.) (您可以根据自己的意愿将
pack
更改为grid
或place
。)
Then in the function, make a list which has all the data to be returned.然后在 function 中,列出所有要返回的数据。 In your case it would be:
在您的情况下,它将是:
#Add this line at the bottom of the function named `weather_def`
return ['Wetter', ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', time + ':00 Uhr',
temperature, image_alt, '-----------------------------------------']
Create a new function and link it in the GUI file to a button(instead of directly calling weather_def
function).Then type the following code in there:创建一个新的 function 并将其在 GUI 文件中链接到一个按钮(而不是直接调用
weather_def
函数)。然后在其中键入以下代码:
def function_name():
#This function should be in the GUI file
list_from_weather_output = weather_def()
for i in list_from_weather_output:
name_of_text_widget.insert(END, i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.