PyQT4简易信息管理系统 | 寒山烟雨
现在的位置: 首页 > 小试身手 > 正文

PyQT4简易信息管理系统

2014年05月07日 小试身手 ⁄ 共 6412字 ⁄ 字号 评论 2 条 ⁄ 阅读 4,477 views 次

第一次用PyQt做微型开发(?),从中午一直忙到深夜,多多少少有些收获,对于Qt的基本思路有了一个大概的了解。200多行代码,说多不多,说少不少,也是自己第一个较为有用的程序吧。

 Python | 
 
 copy code |
?

001
#coding=utf-8
002
import sys
003
from PyQt4 import QtGui, QtCore
004
import csv
005
import os
006
 
007
class Window( QtGui.QWidget ):
008
    def __init__( self ):
009
        super( Window, self ).__init__()
010
        self.setWindowTitle( u"学生信息管理系统" )
011
        self.resize( 300, 400 )
012
        gridlayout = QtGui.QGridLayout()
013
        Input = QtGui.QPushButton( u"插入学生信息" )
014
        Del = QtGui.QPushButton( u"删除学生信息" )
015
        Change = QtGui.QPushButton( u"修改学生信息" )
016
        Search = QtGui.QPushButton( u"查看学生信息" )
017
        Quit = QtGui.QPushButton( u"退出" )
018
 
019
        gridlayout.addWidget( Input, 1, 0, 1, 2 )
020
        gridlayout.addWidget( Del, 2, 0, 1, 2)
021
        gridlayout.addWidget( Change, 3, 0, 1, 2 )
022
        gridlayout.addWidget( Search, 4, 0, 1, 2 )
023
        gridlayout.addWidget( Quit, 5, 0, 1, 2 )
024
        self.setLayout( gridlayout )
025
 
026
        self.connect(Input,QtCore.SIGNAL("clicked()"),self.Input)
027
        self.connect(Del,QtCore.SIGNAL("clicked()"),self.Del)
028
        self.connect(Quit,QtCore.SIGNAL("clicked()"),self.Quit)
029
        self.connect(Search,QtCore.SIGNAL("clicked()"),self.Search)
030
        self.connect(Change,QtCore.SIGNAL("clicked()"),self.Change)
031
 
032
 
033
 
034
    def Input(self):
035
        InputW=In()
036
        I=InputW.exec_()
037
    def Del(self):
038
        DelW=De()
039
        D=DelW.exec_()
040
    def Quit(self):
041
        self.close()
042
    def Search(self):
043
        SeaW=Se()
044
        S=SeaW.exec_()
045
    def Change(self):
046
        ChaW=Ch()
047
        C=ChaW.exec_()
048
 
049
class Ch(QtGui.QDialog):
050
    def __init__(self):
051
        super(Ch,self).__init__()
052
        self.setWindowTitle(u"修改学生信息")
053
        self.resize(200,300)
054
        self.gridlayout=QtGui.QGridLayout()
055
 
056
        self.Id=QtGui.QLabel("Id")
057
        self.Name=QtGui.QLabel("Name:")
058
        self.Sex=QtGui.QLabel("Sex:")
059
        self.Tel=QtGui.QLabel("Tel:")
060
 
061
        self.QboxId=QtGui.QComboBox()
062
        with open('list.csv','r') as fp:
063
            reader=csv.reader(fp,dialect='excel')
064
            for i in reader:
065
                self.QboxId.addItem(i[0])
066
        self.textName=QtGui.QLineEdit()
067
        self.QboxSex=QtGui.QComboBox()
068
        self.QboxSex.addItem(u'男')
069
        self.QboxSex.addItem(u'女')
070
 
071
 
072
        self.textTel=QtGui.QLineEdit()
073
 
074
        self.reButton=QtGui.QPushButton(u"更新")
075
        self.okButton=QtGui.QPushButton("OK")
076
        self.cancalButton=QtGui.QPushButton("Canel")
077
 
078
        self.gridlayout.addWidget( self.Id , 0, 0)
079
        self.gridlayout.addWidget( self.QboxId , 0, 1 )
080
        self.gridlayout.addWidget( self.reButton,0,3)
081
        self.gridlayout.addWidget( self.Name , 1, 0 )
082
        self.gridlayout.addWidget( self.textName , 1, 1 )
083
        self.gridlayout.addWidget( self.Sex , 2, 0 )
084
        self.gridlayout.addWidget( self.QboxSex , 2, 1 )
085
        self.gridlayout.addWidget( self.Tel , 3, 0 )
086
        self.gridlayout.addWidget( self.textTel , 3, 1 )
087
 
088
        self.gridlayout.addWidget( self.okButton , 4, 3 )
089
        self.gridlayout.addWidget( self.cancalButton , 4, 2 )
090
 
091
        self.connect( self.okButton, QtCore.SIGNAL( 'clicked()' ), self.OnOk )
092
        self.connect( self.reButton, QtCore.SIGNAL( 'clicked()' ), self.F5 )
093
        self.connect( self.cancalButton, QtCore.SIGNAL( 'clicked()' ), self.OnCancel )
094
 
095
        self.setLayout( self.gridlayout )
096
 
097
    def F5(self):
098
        with open('list.csv','r') as fp:
099
            reader=csv.reader(fp,dialect='excel')
100
            for i in reader:
101
                if self.QboxId.currentText()==i[0]:
102
                    self.textName.setText(i[1])
103
                    if i[2]==u"女":
104
                        self.QboxSex.setCurrentIndex(1)
105
                    self.textTel.setText(i[3])
106
 
107
    def OnCancel(self):
108
        self.done( 0 )
109
 
110
    def OnOk(self):
111
        with open('list.csv','r') as fp:
112
            ff=open('list1.csv','w')
113
            reader=csv.reader(fp,dialect='excel')
114
            for i in reader:
115
                if self.QboxId.currentText() ==i[0]:
116
                    s=self.QboxId.currentText()+','+self.textName.text()+','+self.QboxSex.currentText()+','+self.textTel.text()+'\n'
117
                    ff.write(s)
118
                else:
119
                    temp=i[0]+','+i[1]+','+i[2]+','+i[3]+'\n'
120
                    ff.write(temp)
121
        ff.close()
122
        os.remove('list.csv')
123
        os.rename('list1.csv','list.csv')
124
        self.done( 1 )
125
 
126
class Se(QtGui.QDialog):
127
    def __init__(self):
128
        super(Se,self).__init__()
129
        self.setWindowTitle(u"查看学生信息")
130
        count = len(open('list.csv','rU').readlines())
131
        self.MyTable = QtGui.QTableWidget(count,4)
132
        self.MyTable.setHorizontalHeaderLabels(['ID','Name','Sex','Tel'])
133
 
134
        with open('list.csv','r') as fp:
135
            reader=csv.reader(fp,dialect='excel')
136
            k=0
137
            for i in reader:
138
                for j in range(4):
139
                    newItem = QtGui.QTableWidgetItem(i[j])  
140
                    self.MyTable.setItem(k, j, newItem)
141
                k+=1
142
 
143
        layout = QtGui.QHBoxLayout()  
144
        layout.addWidget(self.MyTable)  
145
        self.setLayout(layout)
146
 
147
class De(QtGui.QDialog):
148
    def __init__(self):
149
        super(De,self).__init__()
150
        self.setWindowTitle(u"删除学生信息")
151
        self.resize(100,200)
152
        self.gridlayout=QtGui.QGridLayout()
153
        self.Id=QtGui.QLabel(u"删除的Id:")
154
        self.textId=QtGui.QLineEdit()
155
        self.okButton=QtGui.QPushButton("OK")
156
        self.cancalButton=QtGui.QPushButton("Canel")
157
        self.gridlayout.addWidget( self.Id , 0, 0)
158
        self.gridlayout.addWidget( self.textId , 0, 1 )
159
        self.gridlayout.addWidget( self.okButton , 1, 2 )
160
        self.gridlayout.addWidget( self.cancalButton , 1, 3 )
161
        self.connect( self.okButton, QtCore.SIGNAL( 'clicked()' ), self.OnOk )
162
        self.connect( self.cancalButton, QtCore.SIGNAL( 'clicked()' ), self.OnCancel )
163
        self.setLayout( self.gridlayout )
164
    def OnOk(self):
165
        k=0
166
        with open('list.csv','r') as fp:
167
            ff=open('list1.csv','w')
168
            reader=csv.reader(fp,dialect='excel')
169
            for i in reader:
170
                if self.textId.text() ==i[0]:
171
                    k=1
172
                else:
173
                    temp=i[0]+','+i[1]+','+i[2]+','+i[3]+'\n'
174
                    ff.write(temp)
175
        ff.close()
176
 
177
        if k==1:
178
            QtGui.QMessageBox.about(self,u'About',u'删除成功')
179
        else:
180
            QtGui.QMessageBox.about(self,u'About',u'删除失败')
181
        fp.close()
182
        self.done( 1 )
183
        os.remove('list.csv')
184
        os.rename('list1.csv','list.csv')
185
    def OnCancel(self):
186
        self.done( 0 )
187
 
188
 
189
 
190
 
191
class In(QtGui.QDialog):
192
    def __init__(self):
193
        super(In,self).__init__()
194
        self.setWindowTitle(u"插入学生信息")
195
        self.resize(200,300)
196
        self.gridlayout=QtGui.QGridLayout()
197
 
198
        self.Id=QtGui.QLabel("Id")
199
        self.Name=QtGui.QLabel("Name:")
200
        self.Sex=QtGui.QLabel("Sex:")
201
        self.Tel=QtGui.QLabel("Tel:")
202
 
203
        self.textId=QtGui.QLineEdit()
204
        self.textName=QtGui.QLineEdit()
205
        self.QboxSex=QtGui.QComboBox()
206
        self.QboxSex.addItem(u'男')
207
        self.QboxSex.addItem(u'女')
208
        self.textTel=QtGui.QLineEdit()
209
 
210
 
211
        self.okButton=QtGui.QPushButton("OK")
212
        self.cancalButton=QtGui.QPushButton("Canel")
213
 
214
        self.gridlayout.addWidget( self.Id , 0, 0)
215
        self.gridlayout.addWidget( self.textId , 0, 1 )
216
        self.gridlayout.addWidget( self.Name , 1, 0 )
217
        self.gridlayout.addWidget( self.textName , 1, 1 )
218
        self.gridlayout.addWidget( self.Sex , 2, 0 )
219
        self.gridlayout.addWidget( self.QboxSex , 2, 1 )
220
        self.gridlayout.addWidget( self.Tel , 3, 0 )
221
        self.gridlayout.addWidget( self.textTel , 3, 1 )
222
 
223
        self.gridlayout.addWidget( self.okButton , 4, 3 )
224
        self.gridlayout.addWidget( self.cancalButton , 4, 2 )
225
 
226
 
227
        self.connect( self.okButton, QtCore.SIGNAL( 'clicked()' ), self.OnOk )
228
        self.connect( self.cancalButton, QtCore.SIGNAL( 'clicked()' ), self.OnCancel )
229
 
230
        self.setLayout( self.gridlayout )
231
 
232
    def OnOk( self ):
233
        with open('list.csv',"r") as fp:
234
            k=1
235
            reader=csv.reader(fp,dialect='excel')
236
            for i in reader:
237
                if self.textId.text()==i[0]:
238
                    k=0
239
                    break
240
 
241
        if k==1:
242
            fp=open("list.csv","a")
243
            s=self.textId.text()+','+self.textName.text()+','+self.QboxSex.currentText()+','+self.textTel.text()+'\n'
244
            fp.write(s)
245
            fp.close()
246
            QtGui.QMessageBox.about(self,u'About',u'写入成功')
247
        else:
248
            QtGui.QMessageBox.about(self,u'About',u'Id重复,写入失败')
249
        self.done( 1 )
250
    def OnCancel( self ):
251
        self.done( 0 )
252
 
253
 
254
app = QtGui.QApplication( sys.argv )
255
demo = Window()
256
demo.show()
257
app.exec_()

在win下打包,简易使用cxfreeze,保存成Student.py之后,在cmd中运行:

cxfreeze Student.py

之后会生成dist文件夹,Student.exe主程序和需要的依赖。

0

目前有 2 条留言    访客:2 条, 博主:0 条

  1. 玻璃钢电缆支架 2015年04月28日 下午2:39  @回复  Δ-49楼 回复
    TheWorld Browser TheWorld Browser Windows Windows

    代码写的很正确

  2. 屠龙 2014年05月23日 下午4:25  @回复  Δ-48楼 回复
    Google Chrome Google Chrome Windows Windows

    这些代码不仅多,而且看起来还挺复杂。

给我留言

留言无头像?


×