简体   繁体   English

如何在 tkinter window 中运行 python function?

[英]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更改为gridplace 。)

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.

相关问题 如何在加载 tkinter 窗口后运行函数 - How to run a function after a tkinter window is loaded 如何从第二个 window 在 tkinter 中将 function 作为按钮命令运行 - How to run a function as a button command in tkinter from a 2nd window 如何在新的Tkinter窗口打开时运行可取消的功能 - How to run a cancellable function when a new Tkinter window opens python和Tkinter中的逻辑:如何使窗口无限运行 - Logic within python and Tkinter: how to make a window run endlessly 如何在不使用 tkinter 打开控制台 window 的情况下运行 Python 脚本? - How to make a Python script run without it opening the console window with tkinter? 如何将 python eval() 函数用于 tkinter 文本窗口? - How do I use the python eval() function for a tkinter text window? 如何在 tkinter GUI 窗口 python 中打印类函数的输出? - How to print output from a function of a class in tkinter GUI window python? Python Tkinter-循环前运行窗口 - Python Tkinter - Run window before loop 是否可以在 tkinter 中的定时循环上运行 function 并显示 tkinter Z05B8C74CBD96FBF2DE4C1AF342 - is it possible to run a function on a timed loop in tkinter with the tkinter window already displayed? 如何清除 tkinter (Python) 中的窗口? - How to Clear the window in tkinter (Python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM