python实现360皮肤按钮控件示例

代码如下:

#!/usr/bin/python #-*-coding:utf-8-*-

from pyqt4.qtgui import *from pyqt4.qtcore import *from pyqt4.qt import *

class changeskinwidget(qwidget): def __init__(self,parent = none): super(changeskinwidget,self).__init__(parent) self.setfixedsize(140, 160) self.mouse_press = false self.mouse_enter = false #self.pixmap = qpixmap() self.pixmap_name = “”

self.skin_label = qlabel() #显示皮肤 self.skin_name_label = qlabel() #显示皮肤名称 self.download_count_label = qlabel() #显示下载次数 self.use_skin_button = qpushbutton() #使用此皮肤按钮 self.setcursor(qt.pointinghandcursor)

self.use_skin_button.setstylesheet(“border-radius:3px border:1px solid rgb(180, 190, 200) color:rgb(70, 70, 70) background:transparent”) self.skin_label.setscaledcontents(true) self.skin_label.setfixedsize(100, 65) self.use_skin_button.setfixedsize(85, 25)

self.background_layout = qvboxlayout() self.background_layout.addwidget(self.skin_label, 0, qt.aligncenter) self.background_layout.addwidget(self.skin_name_label, 0, qt.aligncenter) self.background_layout.addwidget(self.download_count_label, 0, qt.aligncenter) self.background_layout.addwidget(self.use_skin_button, 0, qt.aligncenter) self.background_layout.setspacing(5) self.background_layout.setcontentsmargins(0, 10, 0, 10)

self.setlayout(self.background_layout) self.skin.connect(self.changeskin)

self.translatelanguage() skin = pyqtsignal() def changeskin(self, pixmap_name, skin_name, download_count): self.background_name = pixmap_name + “_big.png” self.pixmap_name = self.background_name

#更改皮肤背景 #self.pixmap() self.skin_label.setpixmap(qpixmap(self.background_name))

#更改皮肤名称 self.skin_name_label.settext(skin_name)

#更改下载次数 self.download_count_label.settext(u”download count:” + download_count)

def translatelanguage(self): self.use_skin_button.settext(u”use skin”)

def paintevent(self,event): if(self.mouse_enter): #绘制边框 painter = qpainter(self) pen = qpen(qcolor(210, 225, 230)) painter.setpen(pen) painter.drawroundrect(0,0,self.width()-1, self.height()-1, 5, 5)

def mousepressevent(self,event): #只能是鼠标左键移动和改变大小 if(event.button() == qt.leftbutton): self.mouse_press = true self.emit(signal(“skin”),self.pixmap_name)

def mousereleaseevent(self,event): self.mouse_press = false

def enterevent(self,event): self.mouse_enter = true self.update()

def leaveevent(self,event): self.mouse_enter = false self.update()if __name__ == ‘__main__’: import sys app = qapplication(sys.argv) skin = changeskinwidget() skin.show() sys.exit(app.exec_())

Posted in 未分类