首页 > 代码库 > pyqt一个小例子
pyqt一个小例子
# -*- coding: utf-8 -*-
__author__ = ‘Administrator‘
from PyQt4 import Qt,QtCore,QtGui
import sys,random,operator,decimal,threading,time,winsound
class E_24(QtGui.QDialog):
def __init__(self,parent=None):
super(E_24,self).__init__(parent)
self.setWindowIcon(QtGui.QIcon(‘I/i24.png‘))
self.setStyleSheet("* { margin-top:0pt; "> self.resize(400,300)
self.setWindowTitle(U‘24点测试v1.0版本‘)
labe1=QtGui.QLabel(self)
self.text1=QtGui.QLineEdit(u‘‘,self)#第一个数字
self.text1.setStyleSheet("* { margin-top:0pt; "> labe2=QtGui.QLabel(self)
self.text2=QtGui.QLineEdit(u‘‘,self)#第二个数字
self.text2.setStyleSheet("* { margin-top:0pt; "> labe3=QtGui.QLabel(self)
self.text3=QtGui.QLineEdit(u‘‘,self)#第三个数字
self.text3.setStyleSheet("* { margin-top:0pt; "> labe4=QtGui.QLabel(self)
self.text4=QtGui.QLineEdit(u‘‘,self)#第四个数字
self.text4.setStyleSheet("* { margin-top:0pt; "> labe5=QtGui.QLabel(self)
self.text5=QtGui.QLineEdit(u‘‘,self)#第五个数字
self.text5.setStyleSheet("* { margin-top:0pt; "> self.text6=QtGui.QTextBrowser(self)
self.text6.setStyleSheet("* { margin-top:0pt; "> self.button1=QtGui.QPushButton(u‘确定‘,self)
self.button1.setStyleSheet("* { margin-top:0pt; "> self.button2=QtGui.QPushButton(u‘关闭‘,self)
self.button2.setStyleSheet("* { margin-top:0pt; "> self.button3=QtGui.QPushButton(u‘清除‘,self)
self.button3.setStyleSheet("* { margin-top:0pt; "> self.change1=QtGui.QLabel(u‘你的次数:5次‘,self)
self.change1.setStyleSheet("* { margin-top:0pt; "> self.randon1=QtGui.QPushButton(u‘随机数‘,self)
self.jia=QtGui.QPushButton(‘+‘,self)
self.jian=QtGui.QPushButton(‘-‘,self)
self.zuokouhao=QtGui.QPushButton(‘(‘,self)
self.youkouhao=QtGui.QPushButton(‘)‘,self)
self.cheng=QtGui.QPushButton(‘*‘,self)
self.chu1=QtGui.QPushButton(‘/‘,self)
self.chu2=QtGui.QPushButton(‘//‘,self)
self.chu3=QtGui.QPushButton(‘%‘,self)
self.deyu=QtGui.QPushButton(‘=‘,self)
self.tine=QtGui.QLabel(u‘24点100秒倒计时测试‘)
self.tinex=QtGui.QLabel(u‘100内24点测试‘)
self.startButton=QtGui.QPushButton(‘Start‘)
self.stopButton=QtGui.QPushButton(‘Stop‘)
self.startButton1=QtGui.QPushButton(‘Start1‘)
self.stopButton.setEnabled(False)
self.timer=Timer()##!!!!!!!!!!!!!!!
self.timer2=Timere()##############
self.tine.setFrameStyle(Qt.QFrame.StyledPanel|
Qt.QFrame.Plain)
self.tinex.setFrameStyle(Qt.QFrame.StyledPanel|
Qt.QFrame.Plain)
self.text1.selectAll()
self.button1.setDown(True)
left1=QtGui.QGridLayout()
left1.setSpacing(10)
left1.addWidget(labe1,1,0)
left1.addWidget(self.text1,1,0)
left1.addWidget(labe2,2,0)
left1.addWidget(self.text2,2,0)
left1.addWidget(labe3,3,0)
left1.addWidget(self.text3,3,0)
left1.addWidget(labe4,4,0)
left1.addWidget(self.text4,4,0)
left1.addWidget(labe5,5,0)
left1.addWidget(self.text5,5,0)
left1.addWidget(self.jia,2,1)
left1.addWidget(self.jian,2,2)
left1.addWidget(self.zuokouhao,4,1)
left1.addWidget(self.youkouhao,3,1)
left1.addWidget(self.cheng,3,2)
left1.addWidget(self.tine,6,2)
left1.addWidget(self.chu1,4,2)
left1.addWidget(self.chu2,5,1)
left1.addWidget(self.chu3,5,2)
left1.addWidget(self.text6,6,0)
left1.addWidget(self.button1,7,1)
left1.addWidget(self.button2,7,2)
left1.addWidget(self.change1,1,1)
left1.addWidget(self.button3,8,1)
left1.addWidget(self.randon1,8,2)
left1.addWidget(self.deyu,6,1)
left1.addWidget(self.startButton,1,2)
left1.addWidget(self.stopButton,1,3)
left1.addWidget(self.tinex,2,3)
left1.addWidget(self.startButton1,3,3)
l=QtGui.QVBoxLayout()
l.addLayout(left1)
self.setLayout(l)
QtCore.QObject.connect(self.button3,QtCore.SIGNAL(‘clicked()‘),self.__clear__)
QtCore.QObject.connect(self.button2,QtCore.SIGNAL(‘clicked()‘),self.__close__)
QtCore.QObject.connect(self.text1,QtCore.SIGNAL("textChanged(const QString &)"),self.__update__1)
QtCore.QObject.connect(self.text2,QtCore.SIGNAL("textChanged(const QString &)"),self.__update__2)
QtCore.QObject.connect(self.text3,QtCore.SIGNAL("textChanged(const QString &)"),self.__update__3)
QtCore.QObject.connect(self.text4,QtCore.SIGNAL("textChanged(const QString &)"),self.__update__4)
QtCore.QObject.connect(self.text5,QtCore.SIGNAL("textChanged(const QString &)"),self.__update__5)
QtCore.QObject.connect(self.randon1,QtCore.SIGNAL(‘clicked()‘),self.__random__)
#QtCore.QObject.connect(self.button1,QtCore.SIGNAL(‘clicked()‘),self._ty_)
self.text1.textChanged[str].connect(self._ty_)
self.text2.textChanged[str].connect(self._ty_)
self.text3.textChanged[str].connect(self._ty_)
self.text4.textChanged[str].connect(self._ty_)
self.text5.textChanged[str].connect(self._ty_)
re1=QtCore.QRegExp(‘[0-9+()*/%//=-]*‘)
self.text1.setValidator(QtGui.QRegExpValidator(re1,self))
self.text2.setValidator(QtGui.QRegExpValidator(re1,self))
self.text3.setValidator(QtGui.QRegExpValidator(re1,self))
self.text4.setValidator(QtGui.QRegExpValidator(re1,self))
self.text5.setValidator(QtGui.QRegExpValidator(re1,self))
QtCore.QObject.connect(self.jia,QtCore.SIGNAL(‘clicked()‘),self.__add1)
QtCore.QObject.connect(self.jian,QtCore.SIGNAL(‘clicked()‘),self.__jian)
QtCore.QObject.connect(self.zuokouhao,QtCore.SIGNAL(‘clicked()‘),self.__zuo)
QtCore.QObject.connect(self.youkouhao,QtCore.SIGNAL(‘clicked()‘),self.__you)
QtCore.QObject.connect(self.cheng,QtCore.SIGNAL(‘clicked()‘),self.__cheng)
QtCore.QObject.connect(self.chu1,QtCore.SIGNAL(‘clicked()‘),self.__c1)
QtCore.QObject.connect(self.chu2,QtCore.SIGNAL(‘clicked()‘),self.__c2)
QtCore.QObject.connect(self.chu3,QtCore.SIGNAL(‘clicked()‘),self.__c3)
QtCore.QObject.connect(self.deyu,QtCore.SIGNAL(‘clicked()‘),self.__deyu)
QtCore.QObject.connect(self.button1,QtCore.SIGNAL(‘clicked()‘),self.error)
self.connect(self.startButton, QtCore.SIGNAL("clicked()")
,self.start)
self.connect(self.stopButton, QtCore.SIGNAL("clicked()")
, self.stop)
self.connect(self.timer, QtCore.SIGNAL("updateTime()")
, self.updateTime)
self.setWindowFlags(QtCore.Qt.WindowMaximizeButtonHint)
self.button1.setEnabled(False)
self.button2.setEnabled(False)
self.button3.setEnabled(False)
self.randon1.setEnabled(False)
self.jian.setEnabled(False)
self.jia.setEnabled(False)
self.chu3.setEnabled(False)
self.chu2.setEnabled(False)
self.chu1.setEnabled(False)
self.cheng.setEnabled(False)
self.zuokouhao.setEnabled(False)
self.youkouhao.setEnabled(False)
self.text1.setEnabled(False)
self.text6.setEnabled(False)
self.text4.setEnabled(False)
self.text5.setEnabled(False)
self.text2.setEnabled(False)
self.text3.setEnabled(False)
self.tine.setEnabled(True)
self.text6.setEnabled(False)
self.deyu.setEnabled(False)
self.tinex.setEnabled(False)
self.startButton1.setEnabled(False)
self.connect(self.startButton1, QtCore.SIGNAL("clicked()")
, self.start1)
self.connect(self.stopButton, QtCore.SIGNAL("clicked()")
, self.stop1)
self.connect(self.timer2, QtCore.SIGNAL("updateTime()")
, self.updateTime1)
def updateTime1(self):
self.tinex.setText(u"游戏时间: %s s" % QtCore.QString.number(self.sec))
self.sec += 1
if self.sec>5:
self.tinex.setText(u‘线束‘)
def start1(self):
self.sec = 0
self.startButton1.setEnabled(False)
self.startButton.setEnabled(False)
self.stopButton.setEnabled(True)
self.timer2.start()
def stop1(self):
self.timer2.stop()
self.stopButton.setEnabled(False)
self.startButton.setEnabled(True)
self.startButton1.setEnabled(False)
self.tinex.setText(u"游戏结束")
def updateTime(self):
self.tine.setText(u"离游戏开始还有:%s秒" %QtCore.QString.number(self._time1))
self._time1-=1
if self._time1<0:
self.timer.stop()
self.tine.setText(u‘时间到了,游戏开始‘)
self.tinex.setEnabled(False)
self.startButton.setEnabled(True)
self.stopButton.setEnabled(False)
self.button1.setEnabled(True)
self.button2.setEnabled(True)
self.button3.setEnabled(True)
self.randon1.setEnabled(True)
self.jian.setEnabled(True)
self.jia.setEnabled(True)
self.chu3.setEnabled(True)
self.chu2.setEnabled(True)
self.chu1.setEnabled(True)
self.cheng.setEnabled(True)
self.zuokouhao.setEnabled(True)
self.youkouhao.setEnabled(True)
self.text1.setEnabled(True)
self.text6.setEnabled(True)
self.text4.setEnabled(True)
self.text5.setEnabled(True)
self.text2.setEnabled(True)
self.text3.setEnabled(True)
self.tine.setEnabled(False)
self.text6.setEnabled(True)
self.deyu.setEnabled(True)
self.tinex.setEnabled(True)
self.startButton1.setEnabled(True)
def start(self):
self._time1=5
self.startButton.setEnabled(False)
self.startButton1.setEnabled(False)
self.stopButton.setEnabled(True)
self.timer.start()
def stop(self):
self.timer.stop()
self.stopButton.setEnabled(False)
self.startButton1.setEnabled(True)
self.startButton.setEnabled(True)
self.button1.setEnabled(False)
self.button2.setEnabled(False)
self.button3.setEnabled(False)
self.randon1.setEnabled(False)
self.jian.setEnabled(False)
self.jia.setEnabled(False)
self.chu3.setEnabled(False)
self.chu2.setEnabled(False)
self.chu1.setEnabled(False)
self.cheng.setEnabled(False)
self.zuokouhao.setEnabled(False)
self.youkouhao.setEnabled(False)
self.text1.setEnabled(False)
self.text6.setEnabled(False)
self.text4.setEnabled(False)
self.text5.setEnabled(False)
self.text2.setEnabled(False)
self.text3.setEnabled(False)
self.tine.setEnabled(True)
self.text6.setEnabled(False)
self.deyu.setEnabled(False)
self.startButton1.setEnabled(False)
self.tine.setText(u"重置倒计时时间")
def __deyu(self):
self.text6.append(str(‘=‘))
def __add1(self):
self.text6.append(str(‘+‘))
def __jian(self):
self.text6.append(str(‘-‘))
def __zuo(self):
self.text6.append(str(‘(‘))
def __you(self):
self.text6.append(str(‘)‘))
def __cheng(self):
self.text6.append(str(‘*‘))
def __c1(self):
self.text6.append(str(‘/‘))
def __c2(self):
self.text6.append(str(‘//‘))
def __c3(self):
self.text6.append(str(‘%‘))
def _ty_(self,text):
self.text6.append(text)
self.text6.adjustSize()
def __random__(self):
lst=[self.text1,self.text4,self.text3,self.text5,self.text2]
num1=[1,2,3,4,5,6,7,8,9,0]
if lst!=‘‘:
s=random.sample(num1,5)
self.text1.setText(str(s[0]))
self.text2.setText(str(s[1]))
self.text3.setText(str(s[2]))
self.text4.setText(str(s[3]))
self.text5.setText(str(s[4]))
else:
return
def __update__1(self):
self.button3.setEnabled(True)
def __update__2(self):
self.button3.setEnabled(True)
def __update__3(self):
self.button3.setEnabled(True)
def __update__4(self):
self.button3.setEnabled(True)
def __update__5(self):
self.button3.setEnabled(True)
def __clear__(self):
self.button3.setEnabled(False)
lst=[self.text1,self.text4,self.text3,self.text5,self.text2]
if lst!=0:
self.text2.clear()
self.text5.clear()
self.text3.clear()
self.text1.clear()
self.text4.clear()
self.text6.clear()
self.button3.setEnabled(False)
def __close__(self):
sys.exit(0)
def error(self):
try:
if int(decimal.Decimal(str(self.text1.text()))):
pass
if int(decimal.Decimal(str(self.text2.text()))):
pass
if int(decimal.Decimal(str(self.text3.text()))):
pass
if int(decimal.Decimal(str(self.text4.text()))):
pass
if int(decimal.Decimal(str(self.text5.text()))):
pass
except Exception:
if (self.text1.text())==‘‘:
print QtGui.QMessageBox.information(self,u‘错误提示‘,u‘第一项错误:self.text1,请重新输入‘,2)
elif (self.text2.text())==‘‘:
print QtGui.QMessageBox.information(self,u‘错误提示‘,u‘第二项错误:self.text2,请重新输入‘,2)
elif (self.text3.text())==‘‘:
print QtGui.QMessageBox.information(self,u‘错误提示‘,u‘第三项错误:self.text3,请重新输入‘,2)
elif (self.text4.text())==‘‘:
print QtGui.QMessageBox.information(self,u‘错误提示‘,u‘第四项错误:self.text4,请重新输入‘,2)
elif (self.text5.text())==‘‘:
print QtGui.QMessageBox.information(self,u‘错误提示‘,u‘第五项错误:self.text5,请重新输入‘,2)
class Timer(QtCore.QThread):
def __init__(self, parent=None):
super(Timer, self).__init__(parent)
self.stoped = False
self.mutex = QtCore.QMutex()
def run(self):
with QtCore.QMutexLocker(self.mutex):
self.stoped = False
while True:
if self.stoped:
return
self.emit(QtCore.SIGNAL("updateTime()"))
time.sleep(1)
def stop(self):
with QtCore.QMutexLocker(self.mutex):
self.stoped = True
def isStoped(self):
with QtCore.QMutexLocker(self.mutex):
return self.stoped
class Timere(QtCore.QThread):
def __init__(self, parent=None):
super(Timere, self).__init__(parent)
self.stoped = False
self.mutex = QtCore.QMutex()
def run(self):
with QtCore.QMutexLocker(self.mutex):
self.stoped = False
while True:
if self.stoped:
return
self.emit(QtCore.SIGNAL("updateTime()"))
time.sleep(1)
def stop(self):
with QtCore.QMutexLocker(self.mutex):
self.stoped = True
def isStoped(self):
with QtCore.QMutexLocker(self.mutex):
return self.stoped
app=QtGui.QApplication(sys.argv)
e_24=E_24()
e_24.show()
sys.exit(app.exec_())
如图:
pyqt一个小例子