簡體   English   中英

如何在 tkinter window 中運行 python function?

[英]How to run a python function in a tkinter window?

我使用 Python 和 BeautifulSoup 制作了一些從天氣或新聞等網站獲取信息的功能。 基本上是web刮。 現在我想在不同的 window 和 Tkinter 中運行它們。 例如,我如何在我的代碼中實現天氣 function,以便不僅在我的文本編輯器的終端中,而且在這個 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()

這就是從網站獲取天氣信息的 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('-----------------------------------------')

嗨@jumuell,歡迎來到 Stack Overflow。 我已經看到了評論,但是我正在回答您的問題,因為我發現了其他人沒有的問題。

對於您的問題,您需要顯示多行文本作為結果。 Text小部件允許在其中鍵入多行。 但是邏輯部分和 GUI 部分在不同的文件中。

在 GUI 文件中創建一個新的 Text 小部件。

name_of_text_widget = Text(window)
name_of_text_widget.pack 

(您可以根據自己的意願將pack更改為gridplace 。)

然后在 function 中,列出所有要返回的數據。 在您的情況下,它將是:

#Add this line at the bottom of the function named `weather_def`
return ['Wetter', ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::', time + ':00 Uhr',
temperature, image_alt, '-----------------------------------------']

創建一個新的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM