python - TKinter 画布的透明背景
问题描述
我正在尝试在 Python 中创建一个 GUI 天气应用程序。但是,我无法让文本很好地显示,因为我在主窗口上使用 Canvas 来显示文本,而且我似乎无法让背景透明,而只是纯色。
任何人都有处理这个问题的好方法?
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()
解决方案
推荐阅读
- flutter - Flutter Moor:TypeError:无法读取 null 的属性“getFormTemplates”
- sql-server - 作业失败 作业被调度 1016 调用。运行的最后一步是 step1
- c++ - Linux 文件写入页面缓冲区和多线程 CPU 缓存
- google-apps-script - 当用户发送或编辑时,如何制作通过电子邮件发送 PDF 的脚本
- java - Kafka TestContainer 尝试连接到错误的地址
- python - 使用 python BeautifulSoup,如何找到所有带有 NOT 类的“a”标签
- class - Dart/Flutter - 访问 mixin 或派生类的私有成员
- javascript - React onClick 在第一次点击时不触发,第二次点击很好
- javascript - 变量声明中是否可以有多个 ejs 标签?
- arrays - Excel VBA过滤器到带有条件的数组