python+pyqt实现12306图片验证效果

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现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_()) 

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python文件循环写入行时防止覆盖的解决方法

存在问题: 利用写入代码 with open(r'F:\PythonFiles\PycharmFile\ssq.csv', 'w', encoding='utf-8-sig', n...

详解python和matlab的优势与区别

Python是一种面向对象的解释型计算机程序设计语言。Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议...

python文件拆分与重组实例

python文件拆分与重组实例

文件拆分代码: #-*-encoding:utf-8-*- import os import sys import threading def getFileSi...

pytorch::Dataloader中的迭代器和生成器应用详解

在使用pytorch训练模型,经常需要加载大量图片数据,因此pytorch提供了好用的数据加载工具Dataloader。 为了实现小批量循环读取大型数据集,在Dataloader类具体实...

Python中with及contextlib的用法详解

Python中with及contextlib的用法详解

本文实例讲述了Python中with及contextlib的用法。分享给大家供大家参考,具体如下: 平常Coding过程中,经常使用到的with场景是(打开文件进行文件处理,然后隐式地执...