Python 360 character display interface

Source: Internet
Author: User
This article mainly introduces an example of the python 360 character display interface. For more information, see The code is as follows:


#! /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 # Current image subscript
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 ()] # array for storing images

Self. resize (QSize (WINDOW_WIDTH, WINDOW_HEIGHT ))
Self. setWindowFlags (Qt. FramelessWindowHint)

Self. background_label = QLabel (self) # Background image
Self. background_label.setPixmap (QPixmap ("./img/Character/bg_bottom.png "))
Self. background_label.setGeometry (QRect (0, 0, self. width (), self. height ()))

# Combine four images into one
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) # Image (combination)
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) # Close the button
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) # right shift

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): # shift right
If (self. xpos> = WINDOW_ONEBUTTON_WIDTH ):
If (self. current_index> 0 ):
Self. current_index = self. current_index-1
Self. moveCurrentPage (False) # right shift
Else:
Self. moveCurrentPage (True) # Move left
Else:
Self. moveCurrentPage (True) # Move left
Else: # Move left
If (self. xpos <=-WINDOW_ONEBUTTON_WIDTH ):
If (self. current_index <WINDOW_PAGE_COUNT-1 ):
Self. current_index = self. current_index + 1
Self. moveCurrentPage (True) # Move left
Else:
Self. moveCurrentPage (False) # right shift
Else:
Self. moveCurrentPage (False) # right shift
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)
# Retrieve the icon subscript
Index = 0
For I in range (WINDOW_PAGE_COUNT ):
If (label = self. label_array [I]):
Index = I
Return
# If the subscript is smaller than the current subscript, move it to the left.
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) # pay attention to 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) # right shift

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) # Move left


Def moveCurrentPage (self, direction ):
# Change the button corresponding to the current page
Self. changeCurrentButton ()

# Image Segmentation points
#0-680,680-1360,136 0-2040,204 0-2720
# True: move to left false: move to right

# The possibility of moving left, for the x coordinate
# Index = 0, move the label to-680*0
# Index = 1, move the label to-680*1
# Index = 2, move the label to-680*2
# Index = 3, move the label to-680*3
Self. setLabelMove (False)
Self. current_pos_x = self. total_label.x () # Current label coordinate
Self. dest_pos_x =-WINDOW_WIDTH * self. current_index # target X coordinate
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.exe c _())

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.