欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现360的字符显示界面

程序员文章站 2023-11-30 21:56:04
复制代码 代码如下:#!/usr/bin/python  #-*-coding:utf-8-*- from push_button import *from c...

复制代码 代码如下:

#!/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_())