我正在制作将与MySQL数据库交互的应用程序,以精确地将一个表格与蜜蜂进行交互。我创建主框架,还有3个,一个用于显示表,一个用于插入数据,另一个用于使用id键删除行。当我启动index.py所有3个脚本运行,然后是主窗口。我想从菜单中使用调用脚本。
这是脚本:
import wx
from main import SetupGrid
from insert import InsertData
from delete import DeleteData
class GlavniProzor(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Editor Tabele Setup", size = (800,600))
self.createMenu()
self.CenterOnScreen()
self.Show()
def createMenu(self):
''' Traka menija '''
menubar = wx.MenuBar()
self.SetMenuBar(menubar)
''' Meni Specijalne funkcije '''
menu = wx.Menu()
menubar.Append(menu, '&Specijalne funkcije')
menuitem = wx.MenuItem(menu, wx.ID_ANY, 'Tabela')
#self.Bind(wx.EVT_MENU, self.prikazTabele, menuitem)
menu.AppendItem(menuitem)
menuitem = wx.MenuItem(menu, wx.ID_ANY, 'Unos u tabelu')
# self.Bind(wx.EVT_MENU, self.unosuTabelu, menuitem)
menu.AppendItem(menuitem)
menuitem = wx.MenuItem(menu, wx.ID_ANY, 'Brisanje iz tabele')
# self.Bind(wx.EVT_MENU, self.brisanjeizTabele, menuitem)
menu.AppendItem(menuitem)
menuitem = wx.MenuItem(menu, wx.ID_ANY, 'Izlaz')
# self.Bind(wx.EVT_MENU, self.izlaz, menuitem)
menu.AppendItem(menuitem)
def prikazTabele(self, evt):
dial = SetupGrid(self, -1)
dial.CenterOnParent()
dial.Show()
def unosuTabelu(self, evt):
dial = InsertData(self, -1)
dial.CenterOnParent()
dial.Show()
def brisanjeizTabele(self, evt):
dial = DeleteData(self, -1)
dial.CenterOnParent()
dial.Show()
def izlaz(self, evt):
exit()
app = wx.App(0)
frame = GlavniProzor(None)
app.MainLoop()
这是main.py脚本,显示表
#! /usr/bin/env python
import wx, MySQLdb, wx.lib.intctrl
import wx.grid as gridlib
ID_SETUP = 1
db = MySQLdb.connect("127.0.0.1", "user", "password", "database")
class SetupGrid(wx.Dialog):
def __init__(self, id, title='Tabela Setup'):
wx.Dialog.__init__(self, id, title, size=(1000, 300))
db = MySQLdb.connect("127.0.0.1", "root", "aaa111bbb", "mysqlsetup")
cursor = db.cursor()
def setupid():
sql = 'SELECT idSetup FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setupkomitent():
sql = 'SELECT idKomitent FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setupdrzava():
sql = 'SELECT Drzava FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setupprg():
sql = 'SELECT Prg FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setupprovajder():
sql = 'SELECT Provajder FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setupservername():
sql = 'SELECT ServerName FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setupdatabasename():
sql = 'SELECT DataBaseName FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setupsifarnici():
sql = 'SELECT Sifarnici FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
def setuppromena():
sql = 'SELECT Promena FROM setup'
cursor.execute(sql)
rezltat = cursor.fetchall()
rezultat2 = []
x = 0
for upis in rezltat:
for polje in upis:
users = str(polje)
rezultat2.append(users)
x += 1
return rezultat2
self.idSetup = setupid()
self.idKomitent = setupkomitent()
self.Drzava = setupdrzava()
self.Prg = setupprg()
self.Provajder = setupprovajder()
self.ServerName = setupservername()
self.DataBaseName = setupdatabasename()
self.Sifarnici = setupsifarnici()
self.Promena = setuppromena()
# Define main panel
panel = wx.Panel(self, -1)
vbox = wx.BoxSizer(wx.VERTICAL)
# Define sizers
# Horizontal sizers
SetupTableSizer = wx.BoxSizer(wx.HORIZONTAL)
BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
# Add species table widget
Setup = wx.grid.Grid(panel, -1, size=(1070, 200))
Setup.CreateGrid(9, 9)
Setup.SetColLabelValue(0, "idSetup")
Setup.SetColLabelValue(1, "idKomitent")
Setup.SetColLabelValue(2, "Drzava")
Setup.SetColLabelValue(3, "Prg")
Setup.SetColLabelValue(4, "Provajder")
Setup.SetColLabelValue(5, "ServerName")
Setup.SetColLabelValue(6, "DataBaseName")
Setup.SetColLabelValue(7, "Sifarnici")
Setup.SetColLabelValue(8, "Promena")
Setup.SetRowLabelSize(0)
for i in range(0, len(self.idSetup)):
Setup.SetCellValue(i, 0, self.idSetup[i])
for i in range(0, len(self.idKomitent)):
Setup.SetCellValue(i, 1, self.idKomitent[i])
for i in range(0, len(self.Drzava)):
Setup.SetCellValue(i, 2, self.Drzava[i])
for i in range(0, len(self.Prg)):
Setup.SetCellValue(i, 3, self.Prg[i])
for i in range(0, len(self.Provajder)):
Setup.SetCellValue(i, 4, self.Provajder[i])
for i in range(0, len(self.ServerName)):
Setup.SetCellValue(i, 5, self.ServerName[i])
for i in range(0, len(self.DataBaseName)):
Setup.SetCellValue(i, 6, self.DataBaseName[i])
for i in range(0, len(self.Sifarnici)):
Setup.SetCellValue(i, 7, self.Sifarnici[i])
for i in range(0, len(self.Promena)):
Setup.SetCellValue(i, 8, self.Promena[i])
Setup.AutoSize()
SetupTableSizer.Add(Setup, wx.ALIGN_CENTER | wx.ALL, 0)
panel.SetSizer(vbox)
self.Centre()
self.Show(True)
app = wx.App()
SetupGrid(None, -1)
app.MainLoop()
这是插入脚本
import wx
import MySQLdb
db = MySQLdb.connect("127.0.0.1", "user", "password", "database")
cursor = db.cursor()
class InsertData(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
panel = wx.Panel(self, -1)
panel.SetBackgroundColour('light gray')
#iconFile = "Grocery.ico"
#icon1 = wx.Icon(iconFile, wx.BITMAP_TYPE_ICO)
#self.SetIcon(icon1)
label1 = wx.StaticText(panel, -1, "idKomitent:")
label2 = wx.StaticText(panel, -1, "Drzava:")
label3 = wx.StaticText(panel, -1, "Prg:")
label4 = wx.StaticText(panel, -1, "Provajder:")
label5 = wx.StaticText(panel, -1, "ServerName:")
label6 = wx.StaticText(panel, -1, "DataBaseName:")
label7 = wx.StaticText(panel, -1, "Sifarnici:")
self.idKomitent = wx.TextCtrl(panel, -1, "")
self.Drzava = wx.TextCtrl(panel, -1, "")
self.Prg = wx.TextCtrl(panel, -1, "")
self.Provajder = wx.TextCtrl(panel, -1, "")
self.ServerName = wx.TextCtrl(panel, -1, "")
self.DataBaseName = wx.TextCtrl(panel, -1, "")
self.Sifarnici = wx.TextCtrl(panel, -1, "")
self.calc_btn = wx.Button(panel, -1, 'Insert')
self.calc_btn.Bind(wx.EVT_BUTTON, self.onEnter)
self.close = wx.Button(panel, -1, "Exit")
self.Bind(wx.EVT_BUTTON, self.OnCloseMe)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
# use gridbagsizer for layout of widgets
sizer = wx.GridBagSizer(vgap=6, hgap=6)
sizer.Add(label1, pos=(0, 0))
sizer.Add(self.idKomitent, pos=(0, 2)) # row 0, column 2
sizer.Add(label2, pos=(1, 0))
sizer.Add(self.Drzava, pos=(1, 2))
sizer.Add(label3, pos=(2, 0))
sizer.Add(self.Prg, pos=(2, 2))
sizer.Add(label4, pos=(3, 0))
sizer.Add(self.Provajder, pos=(3, 2))
sizer.Add(label5, pos=(4, 0))
sizer.Add(self.ServerName, pos=(4, 2))
sizer.Add(label6, pos=(5, 0))
sizer.Add(self.DataBaseName, pos=(5, 2))
sizer.Add(label7, pos=(6, 0))
sizer.Add(self.Sifarnici, pos=(6, 2))
sizer.Add(self.calc_btn, pos=(8, 1))
sizer.Add(self.close, pos=(8, 2))
# use boxsizer to add border around sizer
border = wx.BoxSizer()
border.Add(sizer, 0, wx.ALL, 20)
panel.SetSizerAndFit(border)
self.Fit()
def onEnter(self, event):
# get the values from the input widgets
idKomitent = int(self.idKomitent.GetValue())
Drzava = str(self.Drzava.GetValue())
Prg = str(self.Prg.GetValue())
Provajder = str(self.Provajder.GetValue())
ServerName = str(self.ServerName.GetValue())
DataBaseName = str(self.DataBaseName.GetValue())
Sifarnici = str(self.Sifarnici.GetValue())
db = MySQLdb.connect("127.0.0.1", "user", "password", "database")
cursor = db.cursor()
cursor.execute("INSERT INTO setup (idKomitent, Drzava, Prg, Provajder, ServerName, DataBaseName, Sifarnici) VALUES (%s, %s, %s, %s, %s, %s, %s)", (idKomitent, Drzava, Prg, Provajder, ServerName, DataBaseName, Sifarnici))
cursor.execute("commit")
self.idKomitent.Clear()
self.Drzava.Clear()
self.Prg.Clear()
self.Provajder.Clear()
self.ServerName.Clear()
self.DataBaseName.Clear()
self.Sifarnici.Clear()
cursor.close()
def OnCloseMe(self, event):
self.Close(True)
def OnCloseWindow(self, event):
self.Destroy()
app = wx.App()
frame = InsertData(None, -1, "Tabela MySQL Setup")
frame.Show()
app.MainLoop()
最终删除脚本
import wx
import MySQLdb
db = MySQLdb.connect("127.0.0.1", "root", "aaa111bbb", "mysqlsetup")
cursor = db.cursor()
class DeleteData(wx.Frame):
def __init__(self):
wx.Frame.__init__(self)
panel = wx.Panel(self, -1)
panel.SetBackgroundColour('light gray')
#iconFile = "Grocery.ico"
#icon1 = wx.Icon(iconFile, wx.BITMAP_TYPE_ICO)
#self.SetIcon(icon1)
label1 = wx.StaticText(panel, -1, "idSetup:")
self.idSetup = wx.TextCtrl(panel, -1, "")
self.calc_btn = wx.Button(panel, -1, 'Delte')
self.calc_btn.Bind(wx.EVT_BUTTON, self.onEnter)
self.close = wx.Button(panel, -1, "Exit")
self.Bind(wx.EVT_BUTTON, self.OnCloseMe)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
# use gridbagsizer for layout of widgets
sizer = wx.GridBagSizer(vgap=3, hgap=3)
sizer.Add(label1, pos=(0, 0))
sizer.Add(self.idSetup, pos=(0, 2)) # row 0, column 2
sizer.Add(self.calc_btn, pos=(2, 1))
sizer.Add(self.close, pos=(2, 2))
# use boxsizer to add border around sizer
border = wx.BoxSizer()
border.Add(sizer, 0, wx.ALL, 20)
panel.SetSizerAndFit(border)
self.Fit()
def onEnter(self, event):
# get the values from the input widgets
idSetup = int(self.idSetup.GetValue())
db = MySQLdb.connect("127.0.0.1", "user", "password", "database")
cursor = db.cursor()
cursor.execute("DELETE FROM setup WHERE idSetup = '%s'" % (idSetup))
cursor.execute("commit")
self.idSetup.Clear()
cursor.close()
def OnCloseMe(self, event):
self.Close(True)
def OnCloseWindow(self, event):
self.Destroy()
app = wx.App()
frame = DeleteData(None, -1, "Tabela MySQL Setup")
frame.Show()
app.MainLoop()
我犯了错误?
答案 0 :(得分:1)
我认为它可能就像添加:
一样简单SequenceEqual
在true
部分之前即。
if __name__ == "__main__":
注意:使用名称,app=wx.App()
,if __name__ == "__main__":
app = wx.App(0)
frame = GlavniProzor(None)
app.MainLoop()
,index
和main
作为您的程序名称可能是一个坏主意,它们有被解释为任何阅读代码的人都不能通过python自己获取关键字。