所以我正在使用 pygame 制作一个基本的天气应用程序,但我的 gui 有问题
所以所有代码都可以正常工作,但窗口没有正确显示
这是代码:
import ast, sys
import pygame
from pygame.locals import *
import inputbox
import get_info
class Weather():
def __init__(self):
self.zip = '99354'
self.get_info()
pygame.font.init()
self.state = 2
self.font = pygame.font.Font("resources\Sansation_Light.ttf", 16)
self.font_render()
self.bg = pygame.image.load('resources/background.png')
self.refresh = pygame.image.load('resources/button1.png')
self.refresh_r = self.refresh.get_bounding_rect()
self.refresh_r.x, self.refresh_r.y = (155,550)
self.chg_zip = pygame.image.load('resources/button2.png')
self.chg_zip_r = self.chg_zip.get_bounding_rect()
self.chg_zip_r.x, self.chg_zip_r.y = (20,135)
self.main()
def font_render(self):
self.current_img = pygame.image.load('resources/%s.png'%self.weather)
self.cur_temp = self.font.render(self.temp, 0, (255,255,255))
self.cur_weather = self.font.render(self.weather, 0, (255,255,255))
self.updated = self.font.render(self.update[0], 0, (255,255,255))
self.updated2 = self.font.render(self.update[2], 0, (255,255,255))
self.ref_text = self.font.render("Refresh", 0, (255,255,255))
self.zip_text = self.font.render("Change Zip", 0, (255,255,255))
def Draw(self):
self.screen.blit(self.bg,(0,0))
self.screen.blit(self.refresh,(155,550))
self.screen.blit(self.ref_text,(200,560))
self.screen.blit(self.chg_zip,(20,135))
self.screen.blit(self.zip_text,(40,140))
self.screen.blit(self.radar_img,(12,175))
self.screen.blit(self.updated,(100,500))
self.screen.blit(self.updated2,(175,525))
self.screen.blit(self.current_img,(20,20))
self.screen.blit(self.cur_temp,(150,50))
self.screen.blit(self.cur_weather,(150,70))
row = 25
for i in range(0,5):
stat_str = str(self.info[0][i][0])+' : '+ str(self.info[0][i][1])
self.cur_stats = self.font.render(stat_str, 0, (255,255,255))
self.screen.blit(self.cur_stats,(275,row))
row+=25
def button(self):
if self.refresh_r.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
self.get_info()
if self.chg_zip_r.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
self.state = 1
def get_zip(self):
self.screen.blit(self.bg,(0,0))
self.zip = inputbox.ask(self.screen,"Zipcode")
self.__init__()
self.screen.blit(self.bg,(0,0))
self.state = 2
def get_info(self):
get_info.get_info(self.zip)
print "done"
with open('info.txt','r') as info:
fore_info = info.read()
weather = ast.literal_eval(fore_info)
current = weather[0]
self.info = weather[1]
forecast = weather[2]
self.place = weather[3]
self.update = self.place.strip().split('.')
self.temp = current[0]
self.weather = current[1]
self.radar_img = pygame.image.load('radar.jpg')
def main(self):
clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((475,600))
pygame.display.set_caption('Weather')
while True:
clock.tick(45)
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if self.state == 1:
self.get_zip()
else:
self.Draw()
self.button()
Weather()
我知道我的代码很草率而且不是最好的,但它仍然可以工作,除非我运行它时窗口看起来像这样:
它显示了它背后的东西,直到你将另一个窗口拖到它上面或者你最小化和最大化它然后它显示我应该
那么我应该如何更改我的代码以便它弹出并正确显示