我用kivy(一个gui开发模块)制作了一个简单的gui程序,idle运行正常,代码如下:
import os
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager,Screen
class ConnectPage(GridLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.cols = 2
if os.path.isfile("C:/Users/Administrator/Desktop/informations.txt"):
with open("C:/Users/Administrator/Desktop/informations.txt","r") as f:
d = f.read().split(",")
prev_ip = d[0]
prev_port = d[1]
prev_username = d[2]
else:
prev_ip = ""
prev_port = ""
prev_username = ""
self.add_widget(Label(text="IP:"))
self.ip = TextInput(text=prev_ip,multiline=False)
self.add_widget(self.ip)
self.add_widget(Label(text="Port:"))
self.port = TextInput(text=prev_port,multiline=False)
self.add_widget(self.port)
self.add_widget(Label(text="Username:"))
self.username = TextInput(text=prev_username,multiline=False)
self.add_widget(self.username)
self.join = Button(text="Join")
self.join.bind(on_press=self.join_button)
self.add_widget(Label())
self.add_widget(self.join)
def join_button(self,type):
port = self.port.text
ip = self.ip.text
username = self.username.text
with open("C:/Users/Administrator/Desktop/informations.txt","w") as f:
f.write(f"{ip},{port},{username}")
info = f"Attempting to join {ip}:{port} as {username}"
chat_app.info_page.update_info(info)
chat_app.screen_manager.current = "Info"
class InfoPage(GridLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.cols = 1
self.message = Label(halign="center",valign="middle",font_size=30)
self.message.bind(width=self.update_text_width)
self.add_widget(self.message)
def update_info(self,message):
self.message.text = message
def update_text_width(self,*_):
self.message.text_size = (self.message.width*0.9,None)
class EpicApp(App):
def build(self):
self.screen_manager = ScreenManager()
self.connect_page = ConnectPage()
screen = Screen(name="Connect")
screen.add_widget(self.connect_page)
self.screen_manager.add_widget(screen)
self.info_page = InfoPage()
screen = Screen(name="Info")
screen.add_widget(self.info_page)
self.screen_manager.add_widget(screen)
return self.screen_manager
if __name__ == "__main__":
chat_app = EpicApp()
chat_app.run()
当我在命令行输入pyinstaller -F main.py(上面代码文件),打包没有异常。
之后我尝试打开exe,报错了:
(这是我在闪退的瞬间拍到的)
经过一段时间,我按照kivy打包指南,先弄spec文件,修改,然后编译。结果是一样的错误。
我又试着后面加上--hidden-import pyg_resources.py2_warn。
但还是报了同样的错误。
后来,我用了setuptools降级的方法,出现了这个错误
注意,这个main.py的名字我改成了chat_app.py,大家不要留意。
我又试着把pkg_resources的__init__.py的86行删了,报错和上面一样。
有哪位大佬知道如何解决?