lyq------ 2022-02-26 18:32 采纳率: 50%
浏览 56
已结题

wxPython RunTimeError

问题遇到的现象和发生背景

使用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())



  • 写回答

2条回答 默认 最新

  • 陈年椰子 2022-02-28 08:30
    关注

    img

    你的

    if submit:
            return frame.username
    

    这个时候, frame 已经关闭,不存在了。所以就出错了。 你要想返回数据,试下对话框

    dlg = wx.TextEntryDialog(None, "Who is buried in Grant's tomb?",          'A Question', 'Cary Grant')  
    if dlg.ShowModal() == wx.ID_OK:      
        response = dlg.GetValue()  
    

    如果是多窗口,可以用pubsub 库做消息传递

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月21日
  • 已采纳回答 5月13日
  • 创建了问题 2月26日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效