【发布时间】:2018-09-12 22:26:23
【问题描述】:
我用 wxPython 为游戏制作了一个非常简单的 GUI 安装程序。虽然它适用于游戏,但如果您有链接,它在技术上可用于下载和提取任何 zip 文件。问题是当我运行程序时 GUI 冻结。它仍然像它应该的那样下载和提取,但是在发生这种情况时 GUI 完全冻结。除非我可以解冻它,否则我无法更新文本框或制作下载栏。我知道它为什么会冻结我只是不知道如何解决它。有人可以帮帮我吗?
这是我的代码:
import requests, os, sys, zipfile, shutil, subprocess, wx, urllib
url = "{Put any zip file URL here to test the program}"
r = requests.get(url, stream = True)
class Frame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
myPanel = wx.Panel(self,-1)
myButton = wx.Button(myPanel, -1, 'Download', size=(300,50), pos=(40,350))
myButton.Bind(wx.EVT_LEFT_DOWN, self.onClick)
self.Show(True)
def onClick(self, e):
print ('Clicked')
if os.path.exists("RFMB6_WINDOWS"):
print('\n\nRemoving old RFMP files...')
subprocess.check_call(('attrib -R ' + 'RFMB6_WINDOWS' + '\\* /S').split())
shutil.rmtree('RFMB6_WINDOWS')
print('\nRemoved old files.')
else:
pass
print('\n\nDownloading:')
urllib.request.urlretrieve(url, 'RFMP.zip')
print('\nDownload Complete.')
print('\n\nExtracting...')
zip_ref = zipfile.ZipFile("RFMP.zip", 'r')
zip_ref.extractall("RFMB6_WINDOWS")
zip_ref.close()
print('\nExtraction Complete')
print('\n\nCleaning up...')
os.remove("RFMP.zip")
print('\nDone! You have succesfully installed the newest version of the Ravenfield Multiplayer Private Alpha.')
app = wx.App()
frame = Frame(None, wx.ID_ANY, 'Image')
app.MainLoop()
【问题讨论】:
-
下载 zip 文件需要多长时间?它可能会冻结,因为它仍在下载文件。如果让它运行会发生什么?
-
该程序运行良好,但下载时冻结并显示“无响应”。我需要一些方法来多线程它我只是不知道如何。就像我说的,我知道它为什么会冻结,我只需要知道如何解决它。
标签: python python-3.x user-interface wxpython