问题遇到的现象和发生背景
使用Wxpython时报错:
line 72, in OnclickSubmit
RuntimeError: wrapped C/C++ object of type Main has been deleted
问题相关代码(自行忽略图片及数据库)
import wx
import sqlite3 as sql
from hashlib import md5
conn = sql.connect("UserData.db")
submit=False
class Register(wx.Frame):
global conn,submit
def OnclickSubmit(self,event):
if str(md5(str(self.password_input.GetValue()).encode('utf-8')).hexdigest()) != self.password:
wx.StaticText(self.panel, label="两次密码输入不一致", pos=(120, 80)).SetForegroundColour((255, 0, 0))
else:
cursor = conn.cursor()
cursor.execute("insert into UserList(username,password) values(?,?)", (self.username, self.password))
cursor.close()
conn.commit()
submit = True
self.Destroy()
def OnclickCancel(self, event):
submit = False
self.Destroy()
def __init__(self, username, password):
self.password = password
self.username = username
wx.Frame.__init__(self, None, -1, title="注册", pos=(150, 92), size=(300, 185))
self.panel = wx.Panel(self)
wx.StaticText(self.panel, label="你输入了一个不存在的用户名!是否注册?\n用户名:" + username, pos=(20, 20))
wx.StaticText(self.panel, label="请再次输入密码:", pos=(20, 60))
self.password_input = wx.TextCtrl(self.panel, pos=(120, 60), size=(120, 20),
style=wx.TE_PROCESS_ENTER | wx.TE_PASSWORD)
register_click = wx.Button(self.panel, pos=(20, 100), size=(100, 25), label="注册")
register_click.Bind(wx.EVT_BUTTON, self.OnclickSubmit)
cancel_click = wx.Button(self.panel, pos=(140, 100), size=(100, 25), label="取消")
cancel_click.Bind(wx.EVT_BUTTON, self.OnclickCancel)
class Main(wx.Frame):
global conn, submit
def ShowMessage(self, message):
wx.StaticText(self.panel, label=' ' * 50, pos=(230, 160))
wx.StaticText(self.panel, label=message, pos=(230, 160)).SetForegroundColour((255, 0, 0))
def OnclickSubmit(self, event):
global conn, submit
self.username = str(self.username_input.GetValue())
password = str(md5(str(self.password_input.GetValue()).encode('utf-8')).hexdigest())
if self.username == '':
self.ShowMessage("用户名不能为空")
return
cursor = conn.cursor()
cursor.execute("select * from UserList where username = ?", (self.username,))
result = cursor.fetchone()
cursor.close()
if __name__ == '__main__': print(result, self.username, password)
if result:
if result[1] != password:
self.ShowMessage("用户名或密码不正确")
else:
submit = True
self.Destroy()
else:
app = wx.App()
frame = Register(self.username, password)
frame.Show()
app.MainLoop()
print("_____________________________________")
if submit:
self.Destroy()
def OnclickCancel(self, event):
submit = False
self.Destroy()
def OnclickHelp(self, event):
pass
def __init__(self):
wx.Frame.__init__(self, None, -1, title="登录", pos=(30, 30), size=(600, 371))
self.panel = wx.Panel(self)
img = wx.Image("Logo.png", type=wx.BITMAP_TYPE_PNG).ConvertToBitmap()
wx.StaticBitmap(self.panel, -1, img, (200, 10), (img.GetWidth(), img.GetHeight()))
wx.StaticText(self.panel, label="用户名(不存在自动注册):", pos=(85, 100))
self.username_input = wx.TextCtrl(self.panel, pos=(230, 100), size=(200, 20), style=wx.TE_PROCESS_ENTER)
wx.StaticText(self.panel, label="密码(可以为空):", pos=(135, 130))
self.password_input = wx.TextCtrl(self.panel, pos=(230, 130), size=(200, 20),
style=wx.TE_PASSWORD | wx.TE_PROCESS_ENTER)
login_click = wx.Button(self.panel, pos=(150, 250), size=(100, 25), label="登录")
login_click.Bind(wx.EVT_BUTTON, self.OnclickSubmit)
cancel_click = wx.Button(self.panel, pos=(300, 250), size=(100, 25), label="取消")
cancel_click.Bind(wx.EVT_BUTTON, self.OnclickCancel)
help_click = wx.Button(self.panel, label="使用说明(F1)")
help_click.Bind(wx.EVT_BUTTON, self.OnclickHelp)
def main():
app = wx.App()
frame = Main()
frame.Show()
app.MainLoop()
print('----------------------------------')
conn.close()
if submit:
return frame.username
if __name__ == '__main__':
print(main())