python实现360的字符显示界面


Posted in Python onFebruary 21, 2014
#!/usr/bin/python  
#-*-coding:utf-8-*-
from push_button import *
from clabel import *
from common import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.Qt import *

class CharacterWidget(QWidget):
 def __init__(self,parent = None):
  super(CharacterWidget,self).__init__()
  self.mouse_press = False
  self.mouse_move = False
  self.current_index = 0 #当前图片下标
  self.current_pos_x = 0
  #self.name_list = QStringList()
  self.m_mouseSrcPos = QPoint()
  self.m_mouseDstPos = QPoint()
  self.label_move = False
  self.label_array = [CLabel(),CLabel(),CLabel(),CLabel()] #存储图片的数组 
  self.resize(QSize(WINDOW_WIDTH, WINDOW_HEIGHT))
  self.setWindowFlags(Qt.FramelessWindowHint)
  self.background_label =  QLabel(self) #背景图片
  self.background_label.setPixmap(QPixmap("./img/Character/bg_bottom.png"))
  self.background_label.setGeometry(QRect(0, 0, self.width(), self.height()))
  #将4张图片合成一张
  self.pixmap = QPixmap(QSize(self.width()*WINDOW_PAGE_COUNT, WINDOW_HEIGHT)) #
  painter  = QPainter(self.pixmap)
  for i  in range(WINDOW_PAGE_COUNT):
   painter.drawImage(QRect(WINDOW_WIDTH*i, 0, WINDOW_WIDTH, WINDOW_HEIGHT),\
    QImage(QString("./img/Character/desktop_%1").arg(i)))
  self.total_label =  QLabel(self) #图片(结合体)
  self.total_label.resize(self.pixmap.size())
  self.total_label.setPixmap(self.pixmap)
  self.total_label.move(WINDOW_START_X, WINDOW_START_Y)
  self.close_button =  PushButton(self)  #关闭按钮
  self.translateLanguage()
  for i in range(WINDOW_BUTTON_COUNT):
   self.label =  CLabel(self)
   self.label.resize(QSize(155, 45))
   self.label.setPixmap(QPixmap(QString("./img/Character/btn_%1").arg(i)))
   self.label.setText(self.name_list[i])
   self.label.move(8+i*170, 319)
   self.connect(self.label, SIGNAL("clicked()"), self, SLOT("changeCurrentPage(CLabel())"))
   self.label_array[i] = self.label
  self.label_array[0].setMousePressFlag(False)
  self.close_button.loadPixmap("./img/sysButton/close.png")
  self.close_button.move(self.width()-52, 0)
  self.connect(self.close_button, SIGNAL("clicked()"), self, SLOT("close()"))

 def translateLanguage(self):
  self.name_list= [u"function",u"clear cookie",u"triggerman",u"booster"]
  self.close_button.setToolTip(u"close")
 def mousePressEvent(self,event):
  if(event.button() == Qt.LeftButton):
   self.m_mouseSrcPos = event.pos()
   if(self.m_mouseSrcPos.y() <= 40):
    self.mouse_move = True
   else:
    self.current_pos_x = self.total_label.x()
    self.mouse_press = True
  elif(event.button() == Qt.RightButton):
   if(self.label_move):
    if(self.current_index > 0):
     self.current_index = self.current_index-1
     self.moveCurrentPage(False) #右移
 def mouseReleaseEvent(self,event):
  self.xpos = 0
  if (self.mouse_press):
   if (self.label_move):
    self.m_mouseDstPos = event.pos()
    self.xpos = self.m_mouseDstPos.x() - self.m_mouseSrcPos.x()
    if(self.xpos > 0):#右移
     if(self.xpos >= WINDOW_ONEBUTTON_WIDTH):
      if(self.current_index > 0):
       self.current_index = self.current_index-1
       self.moveCurrentPage(False) #右移
      else:
       self.moveCurrentPage(True) #左移
     else:
      self.moveCurrentPage(True) #左移
    else: #左移
     if(self.xpos <= -WINDOW_ONEBUTTON_WIDTH):
      if(self.current_index < WINDOW_PAGE_COUNT-1):
       self.current_index = self.current_index+1
       self.moveCurrentPage(True) #左移
      else:
       self.moveCurrentPage(False) #右移
     else:
      self.moveCurrentPage(False) #右移
    self.mouse_press = False
  elif(self.mouse_move):
   self.mouse_move = False
 def changeCurrentPage(label):
  for i in range(WINDOW_BUTTON_COUNT):
   if(label != self.label_array[i]):
    self.label_array[i].setMousePressFlag(False)
  #获取点击的图标下标
  index = 0
  for i  in range(WINDOW_PAGE_COUNT):
   if(label == self.label_array[i]):
    index = i
    return 
  #若下标小于当前下标右移,否则左移
  if(index < self.current_index):
   while(index != self.current_index):
    self.current_index = self.current_index-1
    self.moveCurrentPage(False)
  elif(index > self.current_index):
   while(index != self.current_index):
    self.current_index = self.current_index+1
    self.moveCurrentPage(True)
 def mouseMoveEvent(self,event):
  x = 10
  if(self.mouse_press):
   if(self.label_move):
    self.m_mouseDstPos = event.pos()
    x = self.m_mouseDstPos.x() - self.m_mouseSrcPos.x()
    self.setLabelMove(False)
    self.total_label.move(self.current_pos_x + x, WINDOW_START_Y)
    self.setLabelMove(True)
  elif(self.mouse_move):
   self.m_mouseDstPos = event.pos()
   self.move(event.pos() + self.m_mouseDstPos - self.m_mouseSrcPos) #注意debug

 def keyPressEvent(self, e):
  if(self.label_move):   
   if e.key() == Qt.Key_Left | e.key() == Qt.Key_Up:
    if(self.current_index > 0):
     self.current_index = self.current_index-1
     self.moveCurrentPage(False) #右移
    elif e.key() == Qt.Key_Down | e.key() == Qt.Key_Right:
     if(self.current_index < WINDOW_PAGE_COUNT-1):
      self.current_index = self.current_index + 1
      self.moveCurrentPage(True) #左移

 def moveCurrentPage(self,direction):
  #改变当前页面对应的按钮
  self.changeCurrentButton()
  #图片的几个分割点
  #0-680, 680-1360, 1360-2040, 2040-2720
  #真:向左移  假:向右移
  #左移的几种可能性,对于x坐标
  #index=0, 将label移动到-680*0
  #index=1, 将label移动到-680*1
  #index=2, 将label移动到-680*2
  #index=3, 将label移动到-680*3
  self.setLabelMove(False)
  self.current_pos_x = self.total_label.x() #当前label坐标
  self.dest_pos_x = -WINDOW_WIDTH * self.current_index #目标X坐标
  if(direction):
   if(self.current_pos_x > self.dest_pos_x):
    self.total_label.move(self.current_pos_x-WINDOW_PAGE_MOVE, WINDOW_START_Y)
    self.current_pos_x = self.total_label.x()
    qApp.processEvents(QEventLoop.AllEvents)
  else:
   if(self.current_pos_x < self.dest_pos_x):
    self.total_label.move(self.current_pos_x+WINDOW_PAGE_MOVE, WINDOW_START_Y)
    self.current_pos_x = self.total_label.x()
    qApp.processEvents(QEventLoop.AllEvents)
  self.total_label.move(self.dest_pos_x, WINDOW_START_Y)
  self.setLabelMove(True)
 def changeCurrentButton(self):
  for i in range(WINDOW_BUTTON_COUNT):
   if(i != self.current_index):
    self.label_array[i].setMousePressFlag(False)
   else:
    self.label_array[i].setMousePressFlag(True)
 def setLabelMove(self,enable):
  self.label_move = enable
if __name__ == '__main__':
 import sys
 app = QApplication(sys.argv)
 Character = CharacterWidget()
 Character.show()
 sys.exit(app.exec_())
Python 相关文章推荐
Python 元类使用说明
Dec 18 Python
Python入门篇之数字
Oct 20 Python
Python实现把xml或xsl转换为html格式
Apr 08 Python
Python遍历目录并批量更换文件名和目录名的方法
Sep 19 Python
Python tkinter模块中类继承的三种方式分析
Aug 08 Python
开源软件包和环境管理系统Anaconda的安装使用
Sep 04 Python
Python程序退出方式小结
Dec 09 Python
Tensorflow 利用tf.contrib.learn建立输入函数的方法
Feb 08 Python
python版大富翁源代码分享
Nov 19 Python
Python3 log10()函数简单用法
Feb 19 Python
Python-ElasticSearch搜索查询的讲解
Feb 25 Python
Python实现字符串匹配的KMP算法
Apr 04 Python
python和pyqt实现360的CLable控件
Feb 21 #Python
python使用7z解压软件备份文件脚本分享
Feb 21 #Python
python发布模块的步骤分享
Feb 21 #Python
python列表操作使用示例分享
Feb 21 #Python
使用python统计文件行数示例分享
Feb 21 #Python
python 多进程通信模块的简单实现
Feb 20 #Python
python使用rabbitmq实现网络爬虫示例
Feb 20 #Python
You might like
PHP 字符截取 解决中文的截取问题,不用mb系列
2009/09/29 PHP
jQuery 源码分析笔记
2011/05/25 PHP
PHP下通过exec获得计算机的唯一标识[CPU,网卡 MAC地址]
2011/06/09 PHP
phpexcel导入excel数据使用方法实例
2013/12/24 PHP
设置php页面编码的两种方法示例介绍
2014/03/03 PHP
php获取apk包信息的方法
2014/08/15 PHP
PHP中怎样防止SQL注入分析
2014/10/23 PHP
详解PHP错误日志的获取方法
2015/07/20 PHP
原生Js实现元素渐隐/渐现(原理为修改元素的css透明度)
2013/06/24 Javascript
浅谈javascript 函数内部属性
2015/01/21 Javascript
jQuery得到多个值只能用取Class ,不能用取ID的方法
2016/12/04 Javascript
PHP自动加载autoload和命名空间的应用小结
2017/12/01 Javascript
js判断节假日实例代码
2017/12/27 Javascript
bootstrap模态框关闭后清除模态框的数据方法
2018/08/10 Javascript
微信小程序 select 下拉框组件功能
2019/09/09 Javascript
vue.js页面加载执行created,mounted的先后顺序说明
2020/11/07 Javascript
[05:02]2014DOTA2 TI中国区预选赛精彩TOPPLAY第三弹
2014/06/25 DOTA
python实现批量下载新浪博客的方法
2015/06/15 Python
python结合selenium获取XX省交通违章数据的实现思路及代码
2016/06/26 Python
python利用urllib和urllib2访问http的GET/POST详解
2017/09/27 Python
Python中的枚举类型示例介绍
2019/01/09 Python
对python 中class与变量的使用方法详解
2019/06/26 Python
Django Rest framework权限的详细用法
2019/07/25 Python
在python Numpy中求向量和矩阵的范数实例
2019/08/26 Python
利用Python绘制有趣的万圣节南瓜怪效果
2019/10/31 Python
纽约手袋品牌:KARA
2018/03/18 全球购物
Famous Footwear加拿大:美国多品牌运动休闲鞋店
2018/12/05 全球购物
Math.round(11.5)等於多少? Math.round(-11.5)等於多少?
2015/01/27 面试题
毕业生毕业总结的自我评价范文
2013/11/02 职场文书
关于运动会的广播稿(10篇)
2014/09/12 职场文书
办公室文员工作自我鉴定
2014/09/19 职场文书
2015年科协工作总结
2015/05/19 职场文书
如何写好闭幕词
2019/04/02 职场文书
python xlwt模块的使用解析
2021/04/13 Python
SpringBoot整合Mybatis Generator自动生成代码
2021/08/23 Java/Android
如何解决flex文本溢出问题小结
2022/07/15 HTML / CSS