我正在尝试创建一个天气应用程序。我有一段代码可以在接下来的7天内从DarkSkyAPI中检索天气信息。打印出来的字符串看起来像Sat clear-day 10
。我要做的是显示文本我是一个标签,但替换我的clear-sky
图标的字符串的clear-sky
部分。
我正在寻找的格式为Sat {icon} 10
from Tkinter import *
from datetime import timedelta, datetime
from PIL import Image, ImageTk
import requests
import json
## Location of Icons used
icon_lookup = {
'clear-day' : "Icons/WeatherIcons/WeatherIcon.png",
}
class Weather(Frame):
def __init__(self, parent, *args, **kwargs):
Frame.__init__(self, parent, bg = 'black')
self.iconLbl = Label(self, bg="black")
self.iconLbl.grid(row = 0, column = 1, sticky = W)
self.icon = ''
self.get_weather()
def get_weather(self):
try:
## Get Week's Weather Values
todayPlusOne = 'Sat clear-day 10'
## Get Icon
icon_id = 'clear-day'
icon2 = None
if icon_id in icon_lookup:
icon2 = icon_lookup[icon_id]
if icon2 is not None:
if self.icon != icon2:
self.icon = icon2
image = Image.open(icon2)
image = image.resize((100, 100), Image.ANTIALIAS)
image = image.convert('RGB')
photo = ImageTk.PhotoImage(image)
self.iconLbl.config(image=photo)
self.iconLbl.image = photo
else:
self.iconLbl.config(image='')
except Exception as e:
print "Error: %s. Cannot get weather." % e
self.after(900000, self.get_weather)
class FullscreenWindow:
def __init__(self):
self.tk = Tk()
self.tk.configure(background='black')
self.topFrame = Frame(self.tk, background = 'black', height = 240)
self.middleFrame = Frame(self.tk, background = 'black', height = 240)
self.bottomFrame = Frame(self.tk, background = 'black', height = 240)
self.topFrame.pack(side = TOP, fill = BOTH)
self.middleFrame.pack(side = TOP, fill = BOTH)
self.bottomFrame.pack(side = TOP, fill = BOTH)
self.state = False
self.tk.bind('<Return>', self.toggle_fullscreen)
#weather
self.weather = Weather(self.topFrame)
self.weather.pack(side = LEFT, anchor = NW, padx = 25, pady = 25)
def toggle_fullscreen(self, event=None):
self.state = not self.state
self.tk.attributes('-fullscreen', self.state)
return 'break'
if __name__ == '__main__':
w = FullscreenWindow()
w.tk.resizable(width=False, height=False)
w.tk.geometry('720x480')
w.tk.mainloop()
这可能吗?
答案 0 :(得分:1)
您不能将图像放在标签中的文本中间;您只能将图像放在一侧(顶部,底部,左侧,右侧)或居中。要同时拥有文本和图像,您需要使用标签的private void button_login(object sender, EventArgs e)
{
MainMenu ss= new MainMenu(textBox1.Text);
this.Hide();
ss.Show();
}
class MainMenu : Form
{
// This is an "Auto-Implemented Property".
// Auto-Implemented Properties are used as a shortcut of what you have done.
// Google them for more information.
public string UserName { get; set; }
private void MainMenu(string userName)
{
this.UserName = userName;
}
}
属性。
要在文本中间放置图像,至少有三个选项: