我已经在这个项目上工作了很长一段时间。它基本上是这个商店保管程序,出于某种原因,每当我试图打开并查看任何供应商的详细信息时,程序在我查看后崩溃。控制台不会让我运行任何其他命令。
以下是完整代码:
#Place imports here
import wx
from sqlite3 import *
#---------------------------------------------------------------------------
text1='Name:'
text2='Address:'
text3='Contact Number:'
text4='Alternate Number:'
text5='Email ID:'
text6='CST Number :'
text7='VAT Number:'
global datalist
#GUI Starts here
class MainMenu(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Main Window',size=(500,600))
mainpanel=wx.Panel(self)
status=self.CreateStatusBar()
wx.Frame.CenterOnScreen(self)
mainmenubar=wx.MenuBar()
filemenu=wx.Menu()
fileitem=filemenu.Append(wx.ID_EXIT,'&Save and Quit','This will quit the application')
mainmenubar.Append(filemenu, '&File')
usermenu=wx.Menu()
useritem1=usermenu.Append(wx.NewId(), '&View User Details', 'This will allow you to see the current user details')
useritem2=usermenu.Append(wx.NewId(), '&Change User Details', 'This will change the existing user details')
mainmenubar.Append(usermenu, '&User')
suppliermenu=wx.Menu()
supplieritem1=suppliermenu.Append(wx.NewId(),'&New Supplier','This will allow you to a new Supplier to the database')
supplieritem2=suppliermenu.Append(wx.NewId(), '&View Supplier Details', 'This will allow you to view any existing supplier details')
supplieritem3=suppliermenu.Append(wx.NewId(), '&Change Supplier Details', 'This will allow you to change the supplier details')
mainmenubar.Append(suppliermenu, '&Supplier')
self.SetMenuBar(mainmenubar)
self.Bind(wx.EVT_MENU,self.ViewUserDetail,useritem1)
self.Bind(wx.EVT_MENU,self.ChangeUserDetail,useritem2)
self.Bind(wx.EVT_MENU, self.OnQuit, fileitem)
self.Bind(wx.EVT_MENU, self.ViewSupplierDetail,supplieritem2)
self.Bind(wx.EVT_MENU, self.NewSupplier,supplieritem1)
def OnQuit(self, e):
self.Close()
conn.commit()
c.close()
def ViewUserDetail(self,e):
global datalist
datalist=[row for row in c.execute('SELECT * FROM User') ]
self.new=ViewUserDetail(parent=None, id=-1)
self.new.Show()
def ChangeUserDetail(self,e):
self.new=ChangeUserDetail(parent=None, id=-1)
self.new.Show()
def ViewSupplierDetail(self,e):
global datalist2
datalist2=[row for row in c.execute('SELECT * FROM Supplier') ]
self.new=ViewSupplierDetail(parent=None, id=-1)
self.new.Show()
def NewSupplier(self,e):
self.new=NewSupplier(parent=None, id=-1)
self.new.Show()
conn=Connection('E:\Python Software\ShopTest\SystemData.db')
c=conn.cursor()
try:
c.execute('''CREATE TABLE User
(Name, Address, Contact_No, Alternate_No, Email_ID, CST_No, VAT_No, ID Primary Key) ''')
except OperationalError:
pass
try:
c.execute('''CREATE TABLE Supplier
(Name, Address, Contact_No, Alternate_No, Email_ID, CST_No, VAT_No, ID Primary Key) ''')
except OperationalError:
pass
class ChangeUserDetail(wx.MiniFrame):
def __init__(self,parent,id):
wx.MiniFrame.__init__(self,parent,id,'Change User Details',size=(350,490))
wx.MiniFrame.CenterOnScreen(self)
changeuserdetailpanel=wx.Panel(self)
sta = wx.StaticText(changeuserdetailpanel, label=text1, pos=(10,9))
stb = wx.StaticText(changeuserdetailpanel, label=text2, pos=(10,89))
stc = wx.StaticText(changeuserdetailpanel, label=text3, pos=(10,169))
std = wx.StaticText(changeuserdetailpanel, label=text4, pos=(10,219))
ste = wx.StaticText(changeuserdetailpanel, label=text5, pos=(10,269))
stf = wx.StaticText(changeuserdetailpanel, label=text6, pos=(10,319))
stg = wx.StaticText(changeuserdetailpanel, label=text7, pos=(10,369))
global tcl1,tcl2,tcl3,tcl4,tcl5,tcl6,tcl7,tcl8,tcl9
tcl1 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,7))
tcl2 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,57)) #,style=wx.TE_MULTILINE)
tcl3 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,87))
tcl4 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,114))
tcl5 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,167))
tcl6 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,217))
tcl7 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,267))
tcl8 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,317))
tcl9 = wx.TextCtrl(changeuserdetailpanel, -1, "", size=(175, 25),pos=(145,367))
submitbutton=wx.Button(changeuserdetailpanel, label='Submit', pos=(60,410),size=(100,30))
cancelbutton=wx.Button(changeuserdetailpanel, label='Cancel', pos=(180,410),size=(100,30))
self.Bind(wx.EVT_BUTTON,self.OnSubmit,submitbutton)
self.Bind(wx.EVT_BUTTON,self.OnCancel,cancelbutton)
def OnSubmit(self,e):
add1=tcl2.GetValue()+ '\n'
add2=tcl3.GetValue()+ '\n'
add3=tcl4.GetValue()
user_name=tcl1.GetValue()
user_add=add1+add2+add3
user_contact=tcl5.GetValue()
user_alt=tcl6.GetValue()
user_email=tcl7.GetValue()
user_cst=tcl8.GetValue()
user_vat=tcl9.GetValue()
global user_list
user_list=[user_name,user_add,user_contact,user_alt,user_email,user_cst,user_vat,1]
c.execute('DELETE FROM User WHERE ID=1')
c.execute('INSERT INTO User(Name,Address,Contact_No, Alternate_No, Email_ID, CST_No, VAT_No, ID) VALUES(?,?,?,?,?,?,?,?)',user_list)
self.Close(True)
def OnCancel(self,e):
self.Close(True)
class ViewUserDetail(wx.MiniFrame):
def __init__(self,parent,id):
wx.MiniFrame.__init__(self,parent,id,'View User Details',size=(350,390))
wx.MiniFrame.CenterOnScreen(self)
viewuserdetailpanel=wx.Panel(self)
closebutton=wx.Button(viewuserdetailpanel, label='Close', pos=(100,310),size=(100,30))
st1=wx.StaticText(viewuserdetailpanel, label=text1, pos=(10,10))
st2=wx.StaticText(viewuserdetailpanel, label=text2, pos=(10,50))
st3=wx.StaticText(viewuserdetailpanel, label=text3, pos=(10,120))
st4=wx.StaticText(viewuserdetailpanel, label=text4, pos=(10,160))
st5=wx.StaticText(viewuserdetailpanel, label=text5, pos=(10,200))
st6=wx.StaticText(viewuserdetailpanel, label=text6, pos=(10,240))
st7=wx.StaticText(viewuserdetailpanel, label=text7, pos=(10,280))
sta=wx.StaticText(viewuserdetailpanel, label=datalist[0][0], pos=(170,10))
stb=wx.StaticText(viewuserdetailpanel, label=datalist[0][1], pos=(170,50))
stc=wx.StaticText(viewuserdetailpanel, label=datalist[0][2], pos=(170,120))
std=wx.StaticText(viewuserdetailpanel, label=datalist[0][3], pos=(170,160))
ste=wx.StaticText(viewuserdetailpanel, label=datalist[0][4], pos=(170,200))
stf=wx.StaticText(viewuserdetailpanel, label=datalist[0][5], pos=(170,240))
stg=wx.StaticText(viewuserdetailpanel, label=datalist[0][6], pos=(170,280))
self.Bind(wx.EVT_BUTTON,self.OnClose,closebutton)
def OnClose(self,e):
self.Close(True)
class NewSupplier(wx.MiniFrame):
def __init__(self,parent,id):
wx.MiniFrame.__init__(self,parent,id,'New Supplier',size=(350,490))
wx.MiniFrame.CenterOnScreen(self)
newsupplierpanel=wx.Panel(self)
sta = wx.StaticText(newsupplierpanel, label=text1, pos=(10,9))
stb = wx.StaticText(newsupplierpanel, label=text2, pos=(10,89))
stc = wx.StaticText(newsupplierpanel, label=text3, pos=(10,169))
std = wx.StaticText(newsupplierpanel, label=text4, pos=(10,219))
ste = wx.StaticText(newsupplierpanel, label=text5, pos=(10,269))
stf = wx.StaticText(newsupplierpanel, label=text6, pos=(10,319))
stg = wx.StaticText(newsupplierpanel, label=text7, pos=(10,369))
global tcl1,tcl2,tcl3,tcl4,tcl5,tcl6,tcl7,tcl8,tcl9
tcl1 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,7))
tcl2 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,57)) #,style=wx.TE_MULTILINE)
tcl3 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,87))
tcl4 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,114))
tcl5 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,167))
tcl6 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,217))
tcl7 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,267))
tcl8 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,317))
tcl9 = wx.TextCtrl(newsupplierpanel, -1, "", size=(175, 25),pos=(145,367))
submitbutton=wx.Button(newsupplierpanel, label='Submit', pos=(60,410),size=(100,30))
cancelbutton=wx.Button(newsupplierpanel, label='Cancel', pos=(180,410),size=(100,30))
self.Bind(wx.EVT_BUTTON,self.OnSubmit,submitbutton)
self.Bind(wx.EVT_BUTTON,self.OnCancel,cancelbutton)
def OnSubmit(self,e):
add1=tcl2.GetValue()+ '\n'
add2=tcl3.GetValue()+ '\n'
add3=tcl4.GetValue()
supplier_name=tcl1.GetValue()
supplier_add=add1+add2+add3
supplier_contact=tcl5.GetValue()
supplier_alt=tcl6.GetValue()
supplier_email=tcl7.GetValue()
supplier_cst=tcl8.GetValue()
supplier_vat=tcl9.GetValue()
global supplierlist
supplier_list= [supplier_name,supplier_add,supplier_contact,supplier_alt,supplier_email,supplier_cst,supplier_vat]
#c.execute('DELETE FROM User WHERE ID=1')
c.execute('INSERT INTO Supplier(Name,Address,Contact_No, Alternate_No, Email_ID, CST_No, VAT_No, ID) VALUES(?,?,?,?,?,?,?,?)',supplier_list)
self.Close(True)
def OnCancel(self,e):
self.Close(True)
class ViewSupplierDetail(wx.MiniFrame):
def __init__(self,parent,id):
viewsupplierbox=wx.SingleChoiceDialog(None, 'Choose Supplier', 'View Supplier Details', datalist2[0][0])
if viewsupplierbox.ShowModal()==wx.ID_OK:
answer=viewsupplierbox.GetStringSelection()
wx.MiniFrame.__init__(self,parent,id,'View Supplier Details',size=(320,350))
wx.MiniFrame.CenterOnScreen(self)
viewsupplierdetailpanel=wx.Panel(self)
st1=wx.StaticText(viewsupplierdetailpanel, label=text1, pos=(10,10))
st2=wx.StaticText(viewsupplierdetailpanel, label=text2, pos=(10,50))
st3=wx.StaticText(viewsupplierdetailpanel, label=text3, pos=(10,90))
st4=wx.StaticText(viewsupplierdetailpanel, label=text4, pos=(10,130))
st5=wx.StaticText(viewsupplierdetailpanel, label=text5, pos=(10,170))
st6=wx.StaticText(viewsupplierdetailpanel, label=text6, pos=(10,210))
st7=wx.StaticText(viewsupplierdetailpanel, label=text7, pos=(10,250))
closebutton=wx.Button(viewsupplierdetailpanel, label='Ok', pos=(110,270),size=(90,30))
self.Bind(wx.EVT_BUTTON,self.OnClose,closebutton)
def OnClose(self,e):
self.Close(True)
if __name__=='__main__':
app=wx.App()
frame=MainMenu(parent=None,id=-1)
frame.Show()
app.MainLoop()
部分给我带来麻烦的是: class ViewSupplierDetail(wx.MiniFrame):
def __init__(self,parent,id):
viewsupplierbox=wx.SingleChoiceDialog(None, 'Choose Supplier', 'View Supplier Details', datalist2[0][0])
if viewsupplierbox.ShowModal()==wx.ID_OK:
answer=viewsupplierbox.GetStringSelection()
wx.MiniFrame.__init__(self,parent,id,'View Supplier Details',size=(320,350))
wx.MiniFrame.CenterOnScreen(self)
viewsupplierdetailpanel=wx.Panel(self)
st1=wx.StaticText(viewsupplierdetailpanel, label=text1, pos=(10,10))
st2=wx.StaticText(viewsupplierdetailpanel, label=text2, pos=(10,50))
st3=wx.StaticText(viewsupplierdetailpanel, label=text3, pos=(10,90))
st4=wx.StaticText(viewsupplierdetailpanel, label=text4, pos=(10,130))
st5=wx.StaticText(viewsupplierdetailpanel, label=text5, pos=(10,170))
st6=wx.StaticText(viewsupplierdetailpanel, label=text6, pos=(10,210))
st7=wx.StaticText(viewsupplierdetailpanel, label=text7, pos=(10,250))
closebutton=wx.Button(viewsupplierdetailpanel, label='Ok', pos=(110,270),size=(90,30))
self.Bind(wx.EVT_BUTTON,self.OnClose,closebutton)
def OnClose(self,e):
self.Close(True)
任何想法?
答案 0 :(得分:0)
稍微玩了一下之后,我猜测与SQLite的连接永远不会关闭,这可能是挂断。您应该将连接代码放入Main类中,然后将连接对象传递给其实例化中的其他类。然后,每个其他类都可以创建一个游标对象,用于查询,插入,选择等。当您关闭其他帧时,可以清理游标。然后,当您关闭MainMenu类时,可以关闭数据库连接,它应该正确返回。