1. 程式人生 > >python+pyqt實現12306圖片驗證效果

python+pyqt實現12306圖片驗證效果

備份一下 

思路:在滑鼠點選位置加一個按鈕,然後再按鈕中的點選事件中寫一個關閉事件.

#coding:utf-8
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from push_button import *
from PIL import Image

class Yanzheng(QWidget):
    def __init__(self,parent=None):
        super(Yanzheng,self).__init__(parent)
        self.m_start_point=0  #x座標
        self.m_end_point=0  #y座標
        self.coordinate=[]
        self.codeimage="./img/code.png"
        self.connect(self,SIGNAL("addlable"),self.addpic)
        self.main_layout=QFormLayout()
        self.setLayout(self.main_layout)
        self.resize(293,190)
        self.pixmap=QPixmap("./img/cur.png")

    def addpic(self):
        print self.m_start_point,self.m_end_point
        xpoint=self.m_start_point
        ypoint=self.m_end_point-28
        codepng2 = PushButton(self)
        codepng2.loadPixmapreal('./img/cur.png')
        codepng2.setGeometry(self.m_start_point,self.m_end_point,30,30)
        codepng2.show()
        #self.emit(SIGNAL("dellabel"),self.codepng2)
        self.coordinate.append("%s,%s" %(xpoint,ypoint))
        self.connect(codepng2,SIGNAL("clicked()"),lambda:self.dellabel(codepng2,xpoint,ypoint))
        #self.update()
    #刪除標記
    def dellabel(self,q,x,y):
        print x,y
        self.coordinate.remove("%s,%s" %(x,y))
        q.close()

    def mousePressEvent(self,event):
        if (event.type()==QEvent.MouseButtonPress):
            self.m_start_point = event.pos().x()-12
            self.m_end_point= event.pos().y()-12
            self.emit(SIGNAL("addlable"))


    def paintEvent(self, event):
        p = QPainter(self)
        p.drawPixmap(0,0,QPixmap(self.codeimage))


if __name__=='__main__':
    import sys
    app=QApplication(sys.argv)
    inputurl=Yanzheng()
    inputurl.show()
    sys.exit(app.exec_())