2015-05-25 61 views
-1

我正在为我的小型游戏创建启动程序/更新程序。我有程序创建GUI然后让它自动开始下载版本文件,以检查是否需要更新。可惜的是,使用Tkinter制作的gui在文件下载后才会显示。 gui出现后有没有办法让文件下载?在执行程序的其余部分之前显示的Python Tkinter

""" 
Programmer: JR Padfield 
Description: Downloads file/s from a web server 
Version: 1 
Date: 05/16/15 
""" 

import urllib2 
import ConfigParser 
import os 
import hashlib 
from Tkinter import * 
import ttk 
from PIL import Image, ImageTk 


class Launcher(Frame): 
""" Main class for the launcher """ 

def __init__(self, parent): 
    Frame.__init__(self, parent, background="white") 

    self.parent = parent 
    self.parent.title("The World Launcher") 
    self.pack(fill=BOTH, expand=1) 

    # create a lists for the.py checksum 
    self.pymd5 = [] 
    self.pymd5fromini = [] 

    # create a list for the graphics checksum 
    self.graphicsmd5 = [] 
    self.graphicsmd5fromini = [] 

    self.initUI() 

def initUI(self): 
    """ Intializes the gui interface. """ 

    self.image = Image.open('azurekite.jpg') 
    self.tkimage = ImageTk.PhotoImage(self.image) 
    canvas = Canvas(self, width=self.image.size[0]+20, 
        height=self.image.size[1]+20) 
    canvas.create_image(0, 0, anchor='nw', image=self.tkimage) 
    canvas.pack(fill=BOTH, expand=1) 

    self.playButton = Button(self, text="Play The World", command=self.play) 
    self.playButton.place(x=900, y=725) 

    self.forumButton = Button(self, text="Check Forums", command=self.forums) 
    self.forumButton.place(x=780, y=725) 

    self.newsText = Text(self, height=20, width=40) 
    self.newsText.place(x=10, y=10) 
    self.newsText.insert(END, "News Text goes here") 
    self.newsText.config(state=DISABLED) 

    self.updateLabel = Label(self, text="Starting Update Checks...") 
    self.updateLabel.place(x=50, y=680) 

    self.updateBar = ttk.Progressbar(self, orient='horizontal', length=400, mode="determinate") 
    self.updateBar.place(x=50, y=700) 
    self.updateBar["value"] = 0 

    # calls the first check to get things rolling 
    #self.startCheck() 

def play(self): 
    """ Closes the launcher and plays the game """ 

def forums(self): 
    """ Launches the forums """ 

def startCheck(self): 
    self.updateLabel.config(text="Downloading news and version files") 
    try: 
     url = "http://removedrealurl.net/game/updates/version.ini" 
     hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 
     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
     'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 
     'Accept-Encoding': 'none', 
     'Accept-Language': 'en-US,en;q=0.8', 
     'Connection': 'keep-alive'} 

     file_name = url.split('/')[-1] 
     print(file_name) 
     req = urllib2.Request(url, headers=hdr) 
     u = urllib2.urlopen(req) 
     print("opened url") 
     f = open(file_name, "wb") 
     meta = u.info() 
     file_size = int(meta.getheaders("Content-Length")[0]) 
     print "Downloading: %s Bytes: %s" % (file_name, file_size) 
     self.updateBar["maximum"] = file_size 

     file_size_dl = 0 
     block_sz = 8192 
     while True: 
      buffer = u.read(block_sz) 
      if not buffer: 
       break 

      file_size_dl += len(buffer) 
      f.write(buffer) 
      status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100./file_size) 
      status += chr(8) * (len(status) + 1) 
      self.updateBar["value"] = file_size_dl 
      print status 

     f.close() 
    except urllib2.HTTPError, e: 
     print(e) 

    self.updateLabel.config(text="Reading Version file....") 
    config = ConfigParser.ConfigParser() 

    config.read("version.ini") 
    gameweb = config.get('UPDATER', 'GameWebsite') 
    updateurl = config.get('UPDATER', 'updateURL') 
    versionNum = config.get('UPDATER', 'GameWebsite') 

    # # get the pyfiles check sum values. 
    # pycount = config.get('PYFILES', 'COUNT') 
    # 
    # for i in range(pycount): 
    #  self.pymd5fromini.append(config.get('PYFILES', 'pyfile' + i)) 
    # 
    # # get the graphics check sum 
    # gcount = config.getint('GRAPHICS', 'COUNT') 
    # 
    # for i in range(gcount): 
    #  self.graphicsmd5fromini.append(config.get('GRAPHICS', 'graphic' + i)) 


    os.rename("version.ini", "oldVersion.ini") 

def checksum(self): 
    self.updateLabel.config(text="Checking local files....") 
    """ Checks all files to see if they have been changed. """ 
    for file in os.listdir('/TW'): 
     if file.endswith('.py'): 
      # check the md5. open the file to check the contents. 
      with open(file) as checkFile: 
       data = checkFile.read() 
       md5 = hashlib.md5(data).hexdigest() 
       self.pymd5.append(md5) 

    self.updateLabel.config(text="Checking local graphics....") 
    for file in os.listdir('/data/gui'): 
     if file.endswith('.png'): 
      # check the md5. open the file to check the contents. 
      with open(file) as checkFile: 
       data = checkFile.read() 
       md5 = hashlib.md5(data).hexdigest() 
       self.graphicsmd5.append(md5) 

    for file in os.listdir('/data/items'): 
     if file.endswith('.png'): 
      # check the md5. open the file to check the contents. 
      with open(file) as checkFile: 
       data = checkFile.read() 
       md5 = hashlib.md5(data).hexdigest() 
       self.graphicsmd5.append(md5) 

    for file in os.listdir('/data/sprites'): 
     if file.endswith('.png'): 
      # check the md5. open the file to check the contents. 
      with open(file) as checkFile: 
       data = checkFile.read() 
       md5 = hashlib.md5(data).hexdigest() 
       self.graphicsmd5.append(md5) 

    for file in os.listdir('/data/tilesets'): 
     if file.endswith('.png'): 
      # check the md5. open the file to check the contents. 
      with open(file) as checkFile: 
       data = checkFile.read() 
       md5 = hashlib.md5(data).hexdigest() 
       self.graphicsmd5.append(md5) 


def main(): 
""" Launches the program """ 
root = Tk() 
root.geometry('1024x768') 
app = Launcher(root) 
root.mainloop() 

if __name__ == '__main__': 
main() 

回答

0

我想通了,如果你在self.parent.update调用文件下载调用图形用户界面将执行文件下载高清显示之前之前抛出()。

相关问题