TKinter画布的透明背景

2024-04-29 03:33:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Python中创建一个GUI天气应用程序。但是,我无法很好地显示文本,因为我在主窗口上使用画布来显示文本,而且我似乎无法使背景透明,而只是纯色

有人有办法解决这个问题吗

from forecastiopy import *
from geopy.geocoders import Nominatim
from tkinter import *
from PIL import Image

geolocator = Nominatim(user_agent="josephs-application")
# address = input("\nPlease enter an address: ")
address = "Syracuse NY"
location = geolocator.geocode(address)

def city_state_country(coord):
    location = geolocator.reverse(coord, exactly_one=True)
    address = location.raw['address']
    city = address.get('city', '')
    state = address.get('state', '')
    country = address.get('country', '')
    return city, state, country

print(city_state_country("43.0481301, -76.1474244"))

try:
    coordinates = [location.latitude, location.longitude]
except AttributeError:
    print("\n[!] Address not found [!]")

fio = ForecastIO.ForecastIO(apikey,
                            units=ForecastIO.ForecastIO.UNITS_SI,
                            lang=ForecastIO.ForecastIO.LANG_ENGLISH,
                            latitude=coordinates[0], longitude=coordinates[1])

print('\nLatitude', fio.latitude, 'Longitude', fio.longitude)
print('Timezone', fio.timezone, 'Offset', fio.offset)


def convertToF(celcius):
    return (celcius / 5) * 9 + 32


if fio.has_currently() is True:
    currently = FIOCurrently.FIOCurrently(fio)
    daily = FIODaily.FIODaily(fio)

    mainWindow = Tk()
    mainWindow.title("Weather App V.1")
    mainWindow.grid()


    class FullScreenApp(object):
        def __init__(self, master, **kwargs):
            self.master = master
            pad = 3
            self._geom = '200x200+0+0'
            master.geometry("{0}x{1}+0+0".format(
                master.winfo_screenwidth() - pad, master.winfo_screenheight() - pad))
            master.bind('<Escape>', self.toggle_geom)

        def toggle_geom(self, event):
            geom = self.master.winfo_geometry()
            print(geom, self._geom)
            self.master.geometry(self._geom)
            self._geom = geom


    app = FullScreenApp(mainWindow)

    canvas = Canvas(mainWindow, bg="darkgray", height=1920, width=1080)
    filename = PhotoImage(file="images/bg.png")
    background_label = Label(mainWindow, image=filename)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    canvas.grid()

    topLeft = Frame(mainWindow)
    topLeft.place(x=40, y=25, anchor="nw")

    # topLeftCanvas = Canvas(topLeft, width=300, height=200)
    # topLeftCanvas.create_image(100, 50, image=imageFile)
    # topLeftCanvas.create_text(100, 50, text="Hello")
    # topLeftCanvas.grid()


    summaryLabel = Label(topLeft, text=currently.summary)
    summaryLabel.config(font=("Verdana", 30))
    summaryLabel.grid(row=1)

    tempLabel = Label(topLeft, text=str(round(convertToF(currently.temperature), 2)) + "F")
    tempLabel.config(font=("Verdana", 30))
    tempLabel.grid(row=2)

    #TODO: UGLY!! HARDCODED VALUE, FIX!
    topRight = Frame(mainWindow)
    topRight.place(x=1600, y=25, anchor="nw")

    humidityLabel = Label(topRight, text=(str("Humidity\n" + str(currently.humidity) + "%")))
    humidityLabel.config(font=("Verdana", 30))
    humidityLabel.grid(row=1)

    airPressureLabel = Label(topRight, text=(str("Air Pressure\n" + str(currently.pressure) + " ps")))
    airPressureLabel.config(font=("Verdana", 30))
    airPressureLabel.grid(row=2)

    windSpeedLabel = Label(topRight, text=(str("Wind Speed\n" + str(currently.windSpeed) + " mph")))
    windSpeedLabel.config(font=("Verdana", 30))
    windSpeedLabel.grid(row=3)

    mainWindow.mainloop()

Tags: textselfmastercityaddresslocationlabelfio