使用wxPython访问函数时Python崩溃

2024-10-02 18:19:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两段代码。第一个是GUI类:

'''
Created on Mar 6, 2013

@author: Zach
'''
# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Sep  8 2010)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.grid
from Books import *
###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 734,344 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        bSizer2 = wx.BoxSizer( wx.HORIZONTAL )

        self.patrons_table = wx.grid.Grid( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )

        # Grid
        self.patrons_table.CreateGrid( 0, 7 )
        self.patrons_table.EnableEditing( True )
        self.patrons_table.EnableGridLines( True )
        self.patrons_table.SetGridLineColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_APPWORKSPACE ) )
        self.patrons_table.EnableDragGridSize( True )
        self.patrons_table.SetMargins( 0, 0 )

        # Columns
        self.patrons_table.SetColSize( 0, 100 )
        self.patrons_table.SetColSize( 1, 100 )
        self.patrons_table.SetColSize( 2, 100 )
        self.patrons_table.SetColSize( 3, 100 )
        self.patrons_table.SetColSize( 4, 100 )
        self.patrons_table.SetColSize( 5, 100 )
        self.patrons_table.SetColSize( 6, 100 )
        self.patrons_table.EnableDragColMove( True )
        self.patrons_table.EnableDragColSize( True )
        self.patrons_table.SetColLabelSize( 40 )
        self.patrons_table.SetColLabelValue( 0, "ID" )
        self.patrons_table.SetColLabelValue( 1, "Name" )
        self.patrons_table.SetColLabelValue( 2, "Address" )
        self.patrons_table.SetColLabelValue( 3, "Phone" )
        self.patrons_table.SetColLabelValue( 4, "Email" )
        self.patrons_table.SetColLabelValue( 5, "Fees/Day")
        self.patrons_table.SetColLabelValue( 6, "Fees Owed" )

        self.patrons_table.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

        # Rows
        self.patrons_table.AutoSizeRows()
        self.patrons_table.EnableDragRowSize( True )
        self.patrons_table.SetRowLabelSize( 80 )
        self.patrons_table.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

        # Label Appearance

        # Cell Defaults
        self.patrons_table.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
        self.patrons_table.SetToolTipString( u"Table of patrons in the library" )

        bSizer2.Add( self.patrons_table, 7, wx.EXPAND|wx.ALL|wx.ALIGN_BOTTOM, 5 )

        bSizer6 = wx.BoxSizer( wx.VERTICAL )

        self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer6.Add( self.m_panel1, 1, wx.EXPAND |wx.ALL, 5 )

        self.m_button1 = wx.Button( self, wx.ID_ANY, u"Add", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_button1.SetToolTipString( u"Adds a patron" )

        bSizer6.Add( self.m_button1, 0, wx.ALL, 5 )

        self.m_button2 = wx.Button( self, wx.ID_ANY, u"Remove", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_button2.SetToolTipString( u"Removes the selected patron" )

        bSizer6.Add( self.m_button2, 0, wx.ALL, 5 )

        self.m_button3 = wx.Button( self, wx.ID_ANY, u"Update", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_button3.SetToolTipString( u"Updates the database" )

        bSizer6.Add( self.m_button3, 0, wx.ALL, 5 )

        bSizer2.Add( bSizer6, 1, wx.EXPAND, 5 )

        bSizer1.Add( bSizer2, 1, wx.EXPAND, 5 )

        bSizer5 = wx.BoxSizer( wx.HORIZONTAL )

        self.m_searchCtrl2 = wx.SearchCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 715,-1 ), 0 )
        self.m_searchCtrl2.ShowSearchButton( True )
        self.m_searchCtrl2.ShowCancelButton( False )
        bSizer5.Add( self.m_searchCtrl2, 0, wx.ALL|wx.EXPAND, 5 )

        bSizer1.Add( bSizer5, 0, wx.EXPAND, 5 )

        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.patrons_table.Bind( wx.grid.EVT_GRID_CELL_CHANGE, self.onGridChange )
        self.patrons_table.Bind( wx.grid.EVT_GRID_SELECT_CELL, self.onLeftClick )
        self.m_button1.Bind( wx.EVT_BUTTON, self.addpatron )
        self.m_button2.Bind( wx.EVT_BUTTON, self.removepatron )
        self.m_button3.Bind( wx.EVT_BUTTON, self.updatepatronsDatabase )
        self.m_searchCtrl2.Bind( wx.EVT_SEARCHCTRL_SEARCH_BTN, self.searchpatrons )
        self.m_searchCtrl2.Bind( wx.EVT_TEXT_ENTER, self.searchpatrons )


        #Import the Database into the table
        self.lib = Library()
        self.db = Database()
        for i in self.db.getPatrons():
            print "hello"
            self.lib.addPatron(Patron(i[0], i[1], i[2], i[3], i[4]))
        self.lib.resetHistory()


        for i in self.lib.patrons:
            self.patrons_table.InsertRows()
            self.patrons_table.SetCellValue(0,0,str(i["title"]))
            self.patrons_table.SetCellValue(0,1,str(i["author"]))
            self.patrons_table.SetCellValue(0,2,str(i["patron_id"]))
            self.patrons_table.SetCellValue(0,3,str(i["location"]))
            self.patrons_table.SetCellValue(0,4,str(i["publisher"]))
            self.patrons_table.SetCellValue(0,5,str(i["genre"]))
            self.patrons_table.SetCellValue(0,6,str(i["copy_right"]))
            self.patrons_table.SetCellValue(0,7,str(i["subject"]))
            self.patrons_table.SetCellValue(0,8,str(i["is_checked_out"]))
    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class

#EVENTS
    def onGridChange( self, event ):# Called if one of the cell values was changed
        updated_patron = Patron(self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),0),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),1),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),2),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),3),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),4),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),5),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),6),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),7),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),8),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),9),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),10))
        self.lib.editPatron(self.selected_patron, updated_patron)
    def onLeftClick( self, event ): #Turns the selected row into a patron
        self.selected_patron = Patron(self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),0),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),1),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),2),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),3),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),4),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),5),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),6),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),7),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),8),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),9),
                             self.patrons_table.GetCellValue(self.patrons_table.GetGridCursorRow(),10))
        self.row = self.patrons_table.GetGridCursorRow()
        self.column = self.patrons_table.GetGridCursorCol()
        event.Skip()
    def addpatron( self, event ):# adds a patron to the library and the table
        self.patrons_table.InsertRows()
        self.lib.addpatron(Patron())
        event.Skip()
    def removepatron( self, event ): #removes a patron from the table and the library
        self.lib.removepatron(self.selected_patron)
        self.patrons_table.DeleteRows()
        print self.lib.patrons
        event.Skip()
    def updatepatronsDatabase( self, event ): #syncs the database with the library
        self.db.mergeWithLibrary(self.lib)

    def searchpatrons( self, event ):
        value =  self.m_searchCtrl2.GetValue()
        i=0
        while self.patrons_table.GetNumberRows()-1 >i:
            if self.patrons_table.GetCellValue(i,2) == value:
                self.patrons_table.SetGridCursor(i,2)
                self.patrons_table.SelectRow(i)
                self.patrons_table.Scroll(i,i)
                break
            i = i+1
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame1(None)
    frame.Show()
    frame.Maximize()
    app.MainLoop()

代码的第二部分是GUI类背后的引擎:

^{pr2}$

每次运行代码时,控制台中都会弹出所有的print语句标志,但窗口没有打开,然后python崩溃。注释掉我将数据库导入表的for语句可以让程序运行,但是对self.lib.addPatron的任何调用都会再次使程序崩溃。我看这个功能没有问题,是不是我太蠢了?在


Tags: theselfeventaddidlibdeftable
2条回答

当调试在给您任何有用的输出之前崩溃的WXpython程序时,您可以通过在启动应用程序时指定重定向来将错误保存到日志文件中。因此,将启动应用程序的行改为:

app = wx.pySimpleApp(redirect=True,  filename="logfile.txt") 

将您的错误保存到与脚本所在文件夹相同的日志文件中,然后您将更好地了解发生了什么。在

如果你给我们你得到的回溯会有帮助的。当我运行这段代码时,我得到以下结果:

  sqlite3.OperationalError: no such table: patrons
  File "c:\Users\mdriscoll\Desktop\brk.py", line 191, in <module>
    frame = MyFrame1(None)
  File "c:\Users\mdriscoll\Desktop\brk.py", line 115, in __init__
    for i in self.db.getPatrons():
  File "c:\Users\mdriscoll\Desktop\Books.py", line 184, in getPatrons
    self.cursor.execute("SELECT * FROM patrons")

既然你说你也有问题”self.lib.addPatron“,那就意味着这很可能是同一个问题。正如回溯所示,该表不存在。在添加任何操作之前,您需要创建表(即使它们是空的)。本文可能会帮助您入门:http://www.blog.pythonlibrary.org/2012/07/18/python-a-simple-step-by-step-sqlite-tutorial/

相关问题 更多 >