第一次用PyQt做微型开发(?),从中午一直忙到深夜,多多少少有些收获,对于Qt的基本思路有了一个大概的了解。200多行代码,说多不多,说少不少,也是自己第一个较为有用的程序吧。
#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore
import csv
import os
class Window( QtGui.QWidget ):
def __init__( self ):
super( Window, self ).__init__()
self.setWindowTitle( u"学生信息管理系统" )
self.resize( 300, 400 )
gridlayout = QtGui.QGridLayout()
Input = QtGui.QPushButton( u"插入学生信息" )
Del = QtGui.QPushButton( u"删除学生信息" )
Change = QtGui.QPushButton( u"修改学生信息" )
Search = QtGui.QPushButton( u"查看学生信息" )
Quit = QtGui.QPushButton( u"退出" )
gridlayout.addWidget( Input, 1, 0, 1, 2 )
gridlayout.addWidget( Del, 2, 0, 1, 2)
gridlayout.addWidget( Change, 3, 0, 1, 2 )
gridlayout.addWidget( Search, 4, 0, 1, 2 )
gridlayout.addWidget( Quit, 5, 0, 1, 2 )
self.setLayout( gridlayout )
self.connect(Input,QtCore.SIGNAL("clicked()"),self.Input)
self.connect(Del,QtCore.SIGNAL("clicked()"),self.Del)
self.connect(Quit,QtCore.SIGNAL("clicked()"),self.Quit)
self.connect(Search,QtCore.SIGNAL("clicked()"),self.Search)
self.connect(Change,QtCore.SIGNAL("clicked()"),self.Change)
def Input(self):
InputW=In()
I=InputW.exec_()
def Del(self):
DelW=De()
D=DelW.exec_()
def Quit(self):
self.close()
def Search(self):
SeaW=Se()
S=SeaW.exec_()
def Change(self):
ChaW=Ch()
C=ChaW.exec_()
class Ch(QtGui.QDialog):
def __init__(self):
super(Ch,self).__init__()
self.setWindowTitle(u"修改学生信息")
self.resize(200,300)
self.gridlayout=QtGui.QGridLayout()
self.Id=QtGui.QLabel("Id")
self.Name=QtGui.QLabel("Name:")
self.Sex=QtGui.QLabel("Sex:")
self.Tel=QtGui.QLabel("Tel:")
self.QboxId=QtGui.QComboBox()
with open('list.csv','r') as fp:
reader=csv.reader(fp,dialect='excel')
for i in reader:
self.QboxId.addItem(i[0])
self.textName=QtGui.QLineEdit()
self.QboxSex=QtGui.QComboBox()
self.QboxSex.addItem(u'男')
self.QboxSex.addItem(u'女')
self.textTel=QtGui.QLineEdit()
self.reButton=QtGui.QPushButton(u"更新")
self.okButton=QtGui.QPushButton("OK")
self.cancalButton=QtGui.QPushButton("Canel")
self.gridlayout.addWidget( self.Id , 0, 0)
self.gridlayout.addWidget( self.QboxId , 0, 1 )
self.gridlayout.addWidget( self.reButton,0,3)
self.gridlayout.addWidget( self.Name , 1, 0 )
self.gridlayout.addWidget( self.textName , 1, 1 )
self.gridlayout.addWidget( self.Sex , 2, 0 )
self.gridlayout.addWidget( self.QboxSex , 2, 1 )
self.gridlayout.addWidget( self.Tel , 3, 0 )
self.gridlayout.addWidget( self.textTel , 3, 1 )
self.gridlayout.addWidget( self.okButton , 4, 3 )
self.gridlayout.addWidget( self.cancalButton , 4, 2 )
self.connect( self.okButton, QtCore.SIGNAL( 'clicked()' ), self.OnOk )
self.connect( self.reButton, QtCore.SIGNAL( 'clicked()' ), self.F5 )
self.connect( self.cancalButton, QtCore.SIGNAL( 'clicked()' ), self.OnCancel )
self.setLayout( self.gridlayout )
def F5(self):
with open('list.csv','r') as fp:
reader=csv.reader(fp,dialect='excel')
for i in reader:
if self.QboxId.currentText()==i[0]:
self.textName.setText(i[1])
if i[2]==u"女":
self.QboxSex.setCurrentIndex(1)
self.textTel.setText(i[3])
def OnCancel(self):
self.done( 0 )
def OnOk(self):
with open('list.csv','r') as fp:
ff=open('list1.csv','w')
reader=csv.reader(fp,dialect='excel')
for i in reader:
if self.QboxId.currentText() ==i[0]:
s=self.QboxId.currentText()+','+self.textName.text()+','+self.QboxSex.currentText()+','+self.textTel.text()+'\n'
ff.write(s)
else:
temp=i[0]+','+i[1]+','+i[2]+','+i[3]+'\n'
ff.write(temp)
ff.close()
os.remove('list.csv')
os.rename('list1.csv','list.csv')
self.done( 1 )
class Se(QtGui.QDialog):
def __init__(self):
super(Se,self).__init__()
self.setWindowTitle(u"查看学生信息")
count = len(open('list.csv','rU').readlines())
self.MyTable = QtGui.QTableWidget(count,4)
self.MyTable.setHorizontalHeaderLabels(['ID','Name','Sex','Tel'])
with open('list.csv','r') as fp:
reader=csv.reader(fp,dialect='excel')
k=0
for i in reader:
for j in range(4):
newItem = QtGui.QTableWidgetItem(i[j])
self.MyTable.setItem(k, j, newItem)
k+=1
layout = QtGui.QHBoxLayout()
layout.addWidget(self.MyTable)
self.setLayout(layout)
class De(QtGui.QDialog):
def __init__(self):
super(De,self).__init__()
self.setWindowTitle(u"删除学生信息")
self.resize(100,200)
self.gridlayout=QtGui.QGridLayout()
self.Id=QtGui.QLabel(u"删除的Id:")
self.textId=QtGui.QLineEdit()
self.okButton=QtGui.QPushButton("OK")
self.cancalButton=QtGui.QPushButton("Canel")
self.gridlayout.addWidget( self.Id , 0, 0)
self.gridlayout.addWidget( self.textId , 0, 1 )
self.gridlayout.addWidget( self.okButton , 1, 2 )
self.gridlayout.addWidget( self.cancalButton , 1, 3 )
self.connect( self.okButton, QtCore.SIGNAL( 'clicked()' ), self.OnOk )
self.connect( self.cancalButton, QtCore.SIGNAL( 'clicked()' ), self.OnCancel )
self.setLayout( self.gridlayout )
def OnOk(self):
k=0
with open('list.csv','r') as fp:
ff=open('list1.csv','w')
reader=csv.reader(fp,dialect='excel')
for i in reader:
if self.textId.text() ==i[0]:
k=1
else:
temp=i[0]+','+i[1]+','+i[2]+','+i[3]+'\n'
ff.write(temp)
ff.close()
if k==1:
QtGui.QMessageBox.about(self,u'About',u'删除成功')
else:
QtGui.QMessageBox.about(self,u'About',u'删除失败')
fp.close()
self.done( 1 )
os.remove('list.csv')
os.rename('list1.csv','list.csv')
def OnCancel(self):
self.done( 0 )
class In(QtGui.QDialog):
def __init__(self):
super(In,self).__init__()
self.setWindowTitle(u"插入学生信息")
self.resize(200,300)
self.gridlayout=QtGui.QGridLayout()
self.Id=QtGui.QLabel("Id")
self.Name=QtGui.QLabel("Name:")
self.Sex=QtGui.QLabel("Sex:")
self.Tel=QtGui.QLabel("Tel:")
self.textId=QtGui.QLineEdit()
self.textName=QtGui.QLineEdit()
self.QboxSex=QtGui.QComboBox()
self.QboxSex.addItem(u'男')
self.QboxSex.addItem(u'女')
self.textTel=QtGui.QLineEdit()
self.okButton=QtGui.QPushButton("OK")
self.cancalButton=QtGui.QPushButton("Canel")
self.gridlayout.addWidget( self.Id , 0, 0)
self.gridlayout.addWidget( self.textId , 0, 1 )
self.gridlayout.addWidget( self.Name , 1, 0 )
self.gridlayout.addWidget( self.textName , 1, 1 )
self.gridlayout.addWidget( self.Sex , 2, 0 )
self.gridlayout.addWidget( self.QboxSex , 2, 1 )
self.gridlayout.addWidget( self.Tel , 3, 0 )
self.gridlayout.addWidget( self.textTel , 3, 1 )
self.gridlayout.addWidget( self.okButton , 4, 3 )
self.gridlayout.addWidget( self.cancalButton , 4, 2 )
self.connect( self.okButton, QtCore.SIGNAL( 'clicked()' ), self.OnOk )
self.connect( self.cancalButton, QtCore.SIGNAL( 'clicked()' ), self.OnCancel )
self.setLayout( self.gridlayout )
def OnOk( self ):
with open('list.csv',"r") as fp:
k=1
reader=csv.reader(fp,dialect='excel')
for i in reader:
if self.textId.text()==i[0]:
k=0
break
if k==1:
fp=open("list.csv","a")
s=self.textId.text()+','+self.textName.text()+','+self.QboxSex.currentText()+','+self.textTel.text()+'\n'
fp.write(s)
fp.close()
QtGui.QMessageBox.about(self,u'About',u'写入成功')
else:
QtGui.QMessageBox.about(self,u'About',u'Id重复,写入失败')
self.done( 1 )
def OnCancel( self ):
self.done( 0 )
app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()
在win下打包,简易使用cxfreeze,保存成Student.py之后,在cmd中运行:
cxfreeze Student.py
之后会生成dist文件夹,Student.exe主程序和需要的依赖。
代码写的很正确
这些代码不仅多,而且看起来还挺复杂。