|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
先上userManual 再上代码
代码:
- #-*- coding: utf-8 -*-
- from PySide import QtGui,QtCore
- QtCore.QTextCodec.setCodecForTr(QtCore.QTextCodec.codecForName("utf8"))
- import sys
- import ConfigParser
- import os
- COLOGRROUP = ['无边框','红色','黄色','蓝色','绿色','粉红色','白色','紫色','青色','黑色']
- COLORlIST = ['transparent','red','yellow','blue','green',
- 'pink','white','purple','cyan','black']
- SETTING_ERROR = 0
- UPDAT_EERROR = 0
- UPDATE_SUCESS = 1
- MESSAGEINI_SECTION = 'MESSAGE'
- CFGFILENAME = 'userMessage.cfg'
- MESSAGEINIFILENAME = 'message.ini'
- ISINI = 'isIni'
- YEAR = 'year'
- MONTH = 'month'
- DAY = 'day'
- BACKGROUNDCOLOR = 'backgroundColor'
- BACKGROUNDLENGTH = 'backgroundLength'
- BACKGROUNDHEIGHT = 'backgroundHeight'
- WORDSIZE = 'wordSize'
- WORDCOLOR = 'wordColor'
- AUTOADJUSET = 'isAutoAdjust'
- class MainWindow(QtGui.QLabel):
- def __init__(self,parent = None):
- super(MainWindow,self).__init__(parent,
- QtCore.Qt.FramelessWindowHint|QtCore.Qt.WindowSystemMenuHint)
- self.setWindowIcon(QtGui.QIcon('icon.png'))
- self.setWindowTitle(self.tr('多少天了'))
- frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel
- self.setFrameStyle(frameStyle)
- self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
- self.setAlignment(QtCore.Qt.AlignCenter)
- self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
- self.first = True
- self.settingDialog = SettingDialog()
- self.settingDialog.registerCallback(self.changeUiDirectly)
- self.connect(QtCore.SIGNAL("asignal(PyObject)"),self.changeUiDirectly)
- self.iniUi()
- self.first = False
- #self.setGeometry(1235,20,115,50)
- quitAction = QtGui.QAction(self.tr('退出'), self, shortcut="Ctrl+Q",
- triggered=self.close)
- self.addAction(quitAction)
- def iniUi(self):
- if os.path.exists(CFGFILENAME):
- self.readMessageFromFile(CFGFILENAME)
- else:
- self.readMessageFromFile(MESSAGEINIFILENAME)
- #从ini文件或cfg文件中读取初始信息
- def readMessageFromFile(self,fileName):
- self.msgObject = ConfigParser.ConfigParser()
- self.msgObject.read(fileName)
- if self.msgObject.has_section(MESSAGEINI_SECTION):
- isIni = bool(int(self.msgObject.get(MESSAGEINI_SECTION,ISINI))) #写入cfg文件后的数据为str型的,注意!
- year = self.msgObject.get(MESSAGEINI_SECTION ,YEAR)
- month = self.msgObject.get(MESSAGEINI_SECTION,MONTH)
- day = self.msgObject.get(MESSAGEINI_SECTION,DAY)
- backgroundColor = self.msgObject.get(MESSAGEINI_SECTION,BACKGROUNDCOLOR)
- backgroundLength = self.msgObject.get(MESSAGEINI_SECTION,BACKGROUNDLENGTH)
- backgroundHeight = self.msgObject.get(MESSAGEINI_SECTION,BACKGROUNDHEIGHT)
- wordSize = self.msgObject.get(MESSAGEINI_SECTION,WORDSIZE)
- wordColor = self.msgObject.get(MESSAGEINI_SECTION,WORDCOLOR)
- isAutoAdjust = int(self.msgObject.get(MESSAGEINI_SECTION,AUTOADJUSET))
- setting = [int(year),int(month),int(day),int(backgroundColor),\
- int(backgroundLength),int(backgroundHeight),\
- int(wordSize),int(wordColor),bool(isAutoAdjust)]
- self.settingDialog.getUserDateFromCfgFile(setting)
- self.updateUi(isIni,setting)
- else:
- self.writeIniFile()
- def writeIniFile(self):
- iniFile = ConfigParser.ConfigParser()
- iniFile.read(MESSAGEINIFILENAME)
- if not iniFile.has_section(MESSAGEINI_SECTION):
- iniFile.add_section(MESSAGEINI_SECTION)
- iniFile.set(MESSAGEINI_SECTION,ISINI,1)
- iniFile.set(MESSAGEINI_SECTION,YEAR,2013)
- iniFile.set(MESSAGEINI_SECTION,MONTH,11)
- iniFile.set(MESSAGEINI_SECTION,DAY,22)
- iniFile.set(MESSAGEINI_SECTION,BACKGROUNDCOLOR,6)
- iniFile.set(MESSAGEINI_SECTION,BACKGROUNDLENGTH,115)
- iniFile.set(MESSAGEINI_SECTION,BACKGROUNDHEIGHT,50)
- iniFile.set(MESSAGEINI_SECTION,WORDSIZE,19)
- iniFile.set(MESSAGEINI_SECTION,WORDCOLOR,7)
- iniFile.set(MESSAGEINI_SECTION,AUTOADJUSET,1)
- iniFile.write(open(MESSAGEINIFILENAME,"w"))
- self.iniUi()
- def mouseDoubleClickEvent(self,event):
- if self.settingDialog.exec_():
- self.settingDialog.show()
- if self.settingDialog.getIsUpdate():
- if self.updateUi(False,self.settingDialog.createSetting()) == 0:
- if self.settingDialog.exec_():
- self.settingDialog.show()
- if self.settingDialog.getIsUpdate():
- self.updateUi(False,self.settingDialog.createSetting())
- else:
- return
- else:
- self.iniUi()
- def quit(self):
- self.close()
- def mousePressEvent(self, event):
- if event.button() == QtCore.Qt.LeftButton:
- self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
- event.accept()
- def mouseMoveEvent(self, event):
- if event.buttons() == QtCore.Qt.LeftButton:
- self.move(event.globalPos() - self.dragPosition)
- event.accept()
- def changeUiDirectly(self,setting):
- if self.first is not True:
- self.updateUi(False,setting)
- else:
- return
- def updateUi(self,isIni,setting):
- if setting:
- settedDate = [setting[0],setting[1],setting[2]]
- backgroundColorIndex = setting[3]
- backgroundLength = setting[4]
- backgroundHeight = setting[5]
- wordSize = setting[6]
- wordColorIndex = setting[7]
- if isIni:
- self.setText(self.tr('请双击我'))
- else:
- self.setNumber(settedDate)
- self.setBackgroundColor(COLORlIST[backgroundColorIndex])
- self.setWordSize(wordSize)
- self.setWordColor(COLORlIST[wordColorIndex+1])
- if setting[8]:
- self.autoSetBackgroundSize()
- self.settingDialog.disableBackgroundSet()
- else:
- self.resize(int(backgroundLength),int(backgroundHeight))
- self.settingDialog.enableBackgroundSet()
- return UPDATE_SUCESS
- else:
- return UPDAT_EERROR
- #边框自适应文字大小
- def autoSetBackgroundSize(self):
- self.adjustSize()
- def setNumber(self,dateList):
- date = QtCore.QDate(int(dateList[0]),int(dateList[1]),int(dateList[2]))
- currentDate = QtCore.QDate.currentDate()
- self.number = 0
- if date > currentDate:
- while currentDate != date:
- currentDate = QtCore.QDate(currentDate).addDays(1)
- self.number += 1
- else:
- while date != currentDate:
- date = QtCore.QDate(date).addDays(1)
- self.number += 1
- self.setText(str(self.number))
- def setBackgroundColor(self,color):
- if color == 'transparent':
- self.setStyleSheet("background:"+str(color)) #这个是设置的线框
- else:
- self.setStyleSheet("background-color:"+str(color))
- def setWordSize(self,size):
- font = self.font()
- font.setPointSize(size)
- font.setBold(True)
- self.setFont(font)
- def setWordColor(self,color):
- palette = QtGui.QPalette()
- palette.setColor(self.foregroundRole(), color)
- self.setPalette(palette)
- class SettingDialog(QtGui.QDialog):
- def __init__(self,parent = None):
- super(SettingDialog,self).__init__(parent)
- self.isUpdate = None
- layout = QtGui.QGridLayout()
- '''set the beginning time'''
- timeLabel = QtGui.QLabel(self.tr('设定日期:'))
- yearLabel = QtGui.QLabel(self.tr('年'))
- self.yearCombo = QtGui.QSpinBox()
- self.yearCombo.setRange(1949,2050)
- monthLabel = QtGui.QLabel(self.tr('月'))
- self.monthCombo = QtGui.QSpinBox()
- self.monthCombo.setRange(1,12)
- dayLabel = QtGui.QLabel(self.tr('日'))
- self.dayCombo = QtGui.QSpinBox()
- self.dayCombo.setRange(1,31)
- '''set the colour of background'''
- backgroundColorLabel = QtGui.QLabel(self.tr('边框颜色:'))
- self.backgroundColourCombo = QtGui.QComboBox()
- for color in COLOGRROUP:
- self.backgroundColourCombo.addItem(self.tr(color))
- '''set the number size'''
- self.backgroundLabel = QtGui.QLabel(self.tr('边框大小:'))
- self.backgroungLenthLabel = QtGui.QLabel(self.tr('长:'))
- self.backgroundLengthCombo = QtGui.QSpinBox()
- self.backgroundLengthCombo.setFixedSize(70,20)
- self.backgroundLengthCombo.setRange(40,1500)
- self.backgroundHeightLabel = QtGui.QLabel(self.tr('宽:'))
- self.backgroundHeightCombo = QtGui.QSpinBox()
- self.backgroundHeightCombo.setFixedSize(70,20)
- self.backgroundHeightCombo.setRange(20,500)
- self.autoAdjustCheckBox = QtGui.QCheckBox(self.tr("自适应边框"))
- '''set the size of word'''
- wordSizeLabel = QtGui.QLabel(self.tr('字体大小:'))
- self.wordSizeCombo = QtGui.QSpinBox()
- self.wordSizeCombo.setRange(15,400)
- '''set the color of the word'''
- wordColorLabel = QtGui.QLabel(self.tr('字体颜色:'))
- self.wordColorCombo = QtGui.QComboBox()
- for color in COLOGRROUP[1:]:
- self.wordColorCombo.addItem(self.tr(color))
- #数据发生改变,实时显示在ui上
- self.connect(self.yearCombo, QtCore.SIGNAL("valueChanged(int)"),
- self.changeUidata)
- self.connect(self.monthCombo, QtCore.SIGNAL("valueChanged(int)"),
- self.changeUidata)
- self.connect(self.dayCombo, QtCore.SIGNAL("valueChanged(int)"),
- self.changeUidata)
- self.connect(self.backgroundColourCombo, QtCore.SIGNAL("activated(int)"),
- self.changeUidata)
- self.connect(self.wordSizeCombo, QtCore.SIGNAL("valueChanged(int)"),
- self.changeUidata)
- self.connect(self.wordColorCombo, QtCore.SIGNAL("activated(int)"),
- self.changeUidata)
- self.connect(self.backgroundLengthCombo, QtCore.SIGNAL("valueChanged(int)"),
- self.changeUidata)
- self.connect(self.backgroundHeightCombo, QtCore.SIGNAL("valueChanged(int)"),
- self.changeUidata)
- self.connect(self.autoAdjustCheckBox, QtCore.SIGNAL("toggled(bool)"),
- self.changeUidata)
- #布局
- buttonBox = QtGui.QGridLayout()
- label = QtGui.QLabel()
- okButton = QtGui.QPushButton(self.tr('确定'))
- cancelButton = QtGui.QPushButton(self.tr('取消'))
- closeButton = QtGui.QPushButton(self.tr('关闭'))
- okButton.clicked.connect(self.okReturn)
- cancelButton.clicked.connect(self.cancelAction)
- closeButton.clicked.connect(self.cancelAction)
- buttonBox.addWidget(label,0,0,2,1)
- buttonBox.addWidget(okButton,0,1,1,2)
- buttonBox.addWidget(cancelButton,0,3,1,2)
- buttonBox.addWidget(closeButton,0,5,1,2)
- timeBox = QtGui.QGroupBox(self.tr('时间'))
- timeLayout = QtGui.QGridLayout()
- timeLayout.addWidget(timeLabel,0,0,1,4)
- timeLayout.addWidget(self.yearCombo,0,5,1,2)
- timeLayout.addWidget(yearLabel,0,7,1,1)
- timeLayout.addWidget(self.monthCombo,0,8,1,2)
- timeLayout.addWidget(monthLabel,0,10,1,1)
- timeLayout.addWidget(self.dayCombo,0,11,1,2)
- timeLayout.addWidget(dayLabel,0,13,1,1)
- timeBox.setLayout(timeLayout)
- backgroundBox = QtGui.QGroupBox(self.tr('边框'))
- backgroundLayout = QtGui.QGridLayout()
- backgroundLayout.addWidget(backgroundColorLabel,0,0,1,4)
- backgroundLayout.addWidget(self.backgroundColourCombo,0,5,1,4)
- backgroundLayout.addWidget(self.backgroundLabel,2,0,1,2)
- backgroundLayout.addWidget(self.backgroungLenthLabel,2,2,1,1)
- backgroundLayout.addWidget(self.backgroundLengthCombo,2,3,1,1)
- backgroundLayout.addWidget(self.backgroundHeightLabel,2,4,1,1)
- backgroundLayout.addWidget(self.backgroundHeightCombo,2,5,1,2)
- backgroundLayout.addWidget(self.autoAdjustCheckBox,1,0,1,1)
- backgroundBox.setLayout(backgroundLayout)
- wordBox = QtGui.QGroupBox(self.tr('字体'))
- wordLayout = QtGui.QGridLayout()
- wordLayout.addWidget(wordSizeLabel,0,0,1,1)
- wordLayout.addWidget(self.wordSizeCombo,0,1,1,1)
- wordLayout.addWidget(wordColorLabel,1,0,1,1)
- wordLayout.addWidget(self.wordColorCombo,1,1,1,1)
- wordBox.setLayout(wordLayout)
- layout.addWidget(timeBox)
- layout.addWidget(backgroundBox)
- layout.addWidget(wordBox)
- layout.addLayout(buttonBox,3,0)
- self.setLayout(layout)
- self.setWindowTitle(self.tr('设 置'))
- '''触发的函数'''
- #编写callback来实时的控制ui,将设置的参数传递给ui
- def registerCallback(self,callbackFunc):
- self.updateDirectly = callbackFunc
- def changeUidata(self):
- self.updateDirectly(self.createSetting())
- def disableBackgroundSet(self):
- self.backgroundLabel.setDisabled(True)
- self.backgroungLenthLabel.setDisabled(True)
- self.backgroundLengthCombo.setDisabled(True)
- self.backgroundHeightLabel.setDisabled(True)
- self.backgroundHeightCombo.setDisabled(True)
- def enableDisableBackgroundSet(self):
- if self.autoAdjustCheckBox.isChecked():
- self.disableBackgroundSet()
- else:
- self.enableBackgroundSet()
- def enableBackgroundSet(self):
- self.backgroundLabel.setDisabled(False)
- self.backgroungLenthLabel.setDisabled(False)
- self.backgroundLengthCombo.setDisabled(False)
- self.backgroundHeightLabel.setDisabled(False)
- self.backgroundHeightCombo.setDisabled(False)
- #按下确定键后,保存当前设置的数据,关闭设置窗口
- def okReturn(self):
- self.saveData(self.createSetting())
- self.isUpdate = True
- self.close()
- def cancelAction(self):
- self.isUpdate = False
- self.close()
- def createSetting(self):
- year = self.yearCombo.value()
- month = self.monthCombo.value()
- day = self.dayCombo.value()
- backgroundColor = self.backgroundColourCombo.currentIndex()
- backgroundLength = self.backgroundLengthCombo.value()
- backgroundHeight = self.backgroundHeightCombo.value()
- wordSize = self.wordSizeCombo.value()
- wordColor = self.wordColorCombo.currentIndex()
- isAutoBackgroundSet = bool(int(self.autoAdjustCheckBox.isChecked()))
- setting = [int(year),int(month),int(day),int(backgroundColor),\
- int(backgroundLength),int(backgroundHeight),\
- int(wordSize),int(wordColor),isAutoBackgroundSet]
- return setting
- def getIsAutoAdjust(self,isAuto):
- self.autoAdjustCheckBox.setChecked(isAuto)
- self.enableDisableBackgroundSet()
- def getIsUpdate(self):
- return self.isUpdate
- def getIsReset(self):
- return True
- #保存用户数据,编写配置文件
- def saveData(self,setting):
- cfg_obj = ConfigParser.ConfigParser()
- cfg_obj.read(CFGFILENAME)
- if not cfg_obj.has_section(MESSAGEINI_SECTION):
- cfg_obj.add_section(MESSAGEINI_SECTION)
- cfg_obj.set(MESSAGEINI_SECTION,ISINI,int(0))
- cfg_obj.set(MESSAGEINI_SECTION,YEAR,int(setting[0]))
- cfg_obj.set(MESSAGEINI_SECTION,MONTH,int(setting[1]))
- cfg_obj.set(MESSAGEINI_SECTION,DAY,int(setting[2]))
- cfg_obj.set(MESSAGEINI_SECTION,BACKGROUNDCOLOR,int(setting[3]))
- cfg_obj.set(MESSAGEINI_SECTION,BACKGROUNDLENGTH,int(setting[4]))
- cfg_obj.set(MESSAGEINI_SECTION,BACKGROUNDHEIGHT,int(setting[5]))
- cfg_obj.set(MESSAGEINI_SECTION,WORDSIZE,int(setting[6]))
- cfg_obj.set(MESSAGEINI_SECTION,WORDCOLOR,int(setting[7]))
- cfg_obj.set(MESSAGEINI_SECTION,AUTOADJUSET,int(setting[8]))
- cfg_obj.write(open(CFGFILENAME,"w"))
- #获取初始的用户数据,保证用户所看到的配置与对话框中的数据一致
- def getUserDateFromCfgFile(self,setting):
- self.yearCombo.setValue(int(setting[0]))
- self.monthCombo.setValue(int(setting[1]))
- self.dayCombo.setValue(int(setting[2]))
- self.backgroundColourCombo.setCurrentIndex(int(setting[3]))
- self.backgroundLengthCombo.setValue(int(setting[4]))
- self.backgroundHeightCombo.setValue(int(setting[5]))
- self.wordSizeCombo.setValue(int(setting[6]))
- self.wordColorCombo.setCurrentIndex(int(setting[7]))
- self.getIsAutoAdjust(setting[8])
- if __name__ == '__main__':
- app = QtGui.QApplication(sys.argv)
- mainWindow = MainWindow()
- mainWindow.show()
- sys.exit(app.exec_())
复制代码
|
|