Python TKinter画布的透明背景
我正在尝试在Python中创建一个GUI天气应用程序。但是,我无法很好地显示文本,因为我在主窗口上使用画布来显示文本,而且我似乎无法使背景透明,而只是纯色 有人有办法解决这个问题吗Python TKinter画布的透明背景,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在尝试在Python中创建一个GUI天气应用程序。但是,我无法很好地显示文本,因为我在主窗口上使用画布来显示文本,而且我似乎无法使背景透明,而只是纯色 有人有办法解决这个问题吗 from forecastiopy import * from geopy.geocoders import Nominatim from tkinter import * from PIL import Image geolocator = Nominatim(user_agent="josephs-a
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()
来自forecastiopy导入的*
从geopy.geocoders导入提名
从tkinter进口*
从PIL导入图像
地理定位器=提名(用户\代理=“约瑟夫应用程序”)
#地址=输入(“\n请输入地址:”)
地址=“锡拉丘兹纽约”
位置=地理定位器。地理代码(地址)
def城市、州和国家(合作):
位置=地理定位器。反向(坐标,恰好为1=真)
地址=位置。原始['address']
城市=地址。获取('city','')
state=address.get('state','')
国家=地址。获取('country','')
返回城市、州、国家
印刷品(城市、州、国家(“43.0481301,-76.1474244”))
尝试:
坐标=[位置.纬度,位置.经度]
除属性错误外:
打印(“\n[!]未找到地址[!]”)
fio=预测。预测(apikey,
单位=ForecastIO.ForecastIO.units\u SI,
lang=ForecastIO.ForecastIO.lang\u英语,
纬度=坐标[0],经度=坐标[1])
打印('\n纬度'、飞行纬度'、经度'、飞行经度)
打印('Timezone',fio.Timezone',Offset',fio.Offset)
def转换器TOF(celcius):
返回(塞尔西厄斯/5)*9+32
如果fio.has_current()为真:
当前=当前。当前(fio)
每日=假日。假日(fio)
mainWindow=Tk()
主窗口标题(“天气应用程序V.1”)
mainWindow.grid()
类FullScreenApp(对象):
定义初始(自我、主控、**kwargs):
self.master=master
pad=3
自洽性_geom='200x200+0+0'
几何体(“{0}x{1}+0+0”。格式(
master.winfo_屏幕宽度()-pad,master.winfo_屏幕高度()-pad))
主绑定(“”,自切换)
def切换_geom(自身、事件):
geom=self.master.winfo_geometry()
打印(几何图形、自/几何图形)
自学几何(自学几何)
自。_geom=geom
app=FullScreenApp(主窗口)
画布=画布(主窗口,bg=“darkgray”,高度=1920,宽度=1080)
filename=PhotoImage(file=“images/bg.png”)
背景\标签=标签(主窗口,图像=文件名)
背景标签位置(x=0,y=0,relwidth=1,relheight=1)
canvas.grid()
左上=框架(主窗口)
左上角。放置(x=40,y=25,锚定=“nw”)
#topLeftCanvas=画布(topLeft,宽度=300,高度=200)
#topLeftCanvas.create_image(100,50,image=imageFile)
#topLeftCanvas.create_text(100,50,text=“Hello”)
#topLeftCanvas.grid()
summaryLabel=标签(左上角,文本=当前.summary)
summaryLabel.config(字体=(“Verdana”,30))
summaryLabel.grid(行=1)
tempLabel=Label(左上角,text=str(圆形(convertToF(当前温度),2))+“F”)
tempLabel.config(字体=(“Verdana”,30))
tempLabel.grid(行=2)
#托多:丑!!硬编码值,修复!
右上=框架(主窗口)
右上角。放置(x=1600,y=25,锚定=“nw”)
humidityLabel=标签(右上角,文本=(str(“湿度\n”+str(当前为.湿度)+“%”))
humidityLabel.config(字体=(“Verdana”,30))
humidityLabel.grid(行=1)
airPressureLabel=标签(右上角,文本=(str(“气压\n”+str(当前为.Pressure)+“ps”))
airPressureLabel.config(字体=(“Verdana”,30))
airPressureLabel.grid(行=2)
windSpeedLabel=标签(右上角,文本=(str(“风速”+str(当前为.windSpeed)+“mph”))
windSpeedLabel.config(字体=(“Verdana”,30))
windSpeedLabel.grid(行=3)
mainWindow.mainloop()
我认为你不应该在代码中给出API密钥。这里的答案有帮助吗?为什么要在画布上添加Label
小部件而不是文本对象?您到底想要什么,画布的透明背景还是标签?