鱼C论坛

 找回密码
 立即注册
查看: 2031|回复: 4

[作品展示] 发一个带界面的记日子小程序pyside

[复制链接]
发表于 2014-10-30 16:40:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
先上userManual 再上代码
代码:







  1. #-*- coding: utf-8 -*-
  2. from PySide import QtGui,QtCore
  3. QtCore.QTextCodec.setCodecForTr(QtCore.QTextCodec.codecForName("utf8"))
  4. import sys
  5. import ConfigParser
  6. import os
  7. COLOGRROUP = ['无边框','红色','黄色','蓝色','绿色','粉红色','白色','紫色','青色','黑色']
  8. COLORlIST = ['transparent','red','yellow','blue','green',
  9.              'pink','white','purple','cyan','black']
  10. SETTING_ERROR = 0
  11. UPDAT_EERROR = 0
  12. UPDATE_SUCESS = 1
  13. MESSAGEINI_SECTION = 'MESSAGE'
  14. CFGFILENAME = 'userMessage.cfg'
  15. MESSAGEINIFILENAME = 'message.ini'
  16. ISINI = 'isIni'
  17. YEAR = 'year'
  18. MONTH = 'month'
  19. DAY = 'day'
  20. BACKGROUNDCOLOR = 'backgroundColor'
  21. BACKGROUNDLENGTH = 'backgroundLength'
  22. BACKGROUNDHEIGHT = 'backgroundHeight'
  23. WORDSIZE = 'wordSize'
  24. WORDCOLOR = 'wordColor'
  25. AUTOADJUSET = 'isAutoAdjust'

  26. class MainWindow(QtGui.QLabel):
  27.     def __init__(self,parent = None):
  28.         super(MainWindow,self).__init__(parent,
  29.                         QtCore.Qt.FramelessWindowHint|QtCore.Qt.WindowSystemMenuHint)

  30.         self.setWindowIcon(QtGui.QIcon('icon.png'))
  31.         self.setWindowTitle(self.tr('多少天了'))


  32.         frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel

  33.         self.setFrameStyle(frameStyle)
  34.         self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
  35.         self.setAlignment(QtCore.Qt.AlignCenter)
  36.         self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)

  37.         self.first = True
  38.         self.settingDialog = SettingDialog()
  39.         self.settingDialog.registerCallback(self.changeUiDirectly)
  40.         self.connect(QtCore.SIGNAL("asignal(PyObject)"),self.changeUiDirectly)
  41.         self.iniUi()
  42.         self.first = False
  43.         #self.setGeometry(1235,20,115,50)


  44.         quitAction = QtGui.QAction(self.tr('退出'), self, shortcut="Ctrl+Q",
  45.                 triggered=self.close)
  46.         self.addAction(quitAction)

  47.     def iniUi(self):
  48.         if os.path.exists(CFGFILENAME):
  49.             self.readMessageFromFile(CFGFILENAME)
  50.         else:
  51.             self.readMessageFromFile(MESSAGEINIFILENAME)


  52.     #从ini文件或cfg文件中读取初始信息
  53.     def readMessageFromFile(self,fileName):
  54.         self.msgObject = ConfigParser.ConfigParser()
  55.         self.msgObject.read(fileName)
  56.         if self.msgObject.has_section(MESSAGEINI_SECTION):
  57.             isIni = bool(int(self.msgObject.get(MESSAGEINI_SECTION,ISINI)))  #写入cfg文件后的数据为str型的,注意!
  58.             year = self.msgObject.get(MESSAGEINI_SECTION ,YEAR)
  59.             month = self.msgObject.get(MESSAGEINI_SECTION,MONTH)
  60.             day = self.msgObject.get(MESSAGEINI_SECTION,DAY)
  61.             backgroundColor = self.msgObject.get(MESSAGEINI_SECTION,BACKGROUNDCOLOR)
  62.             backgroundLength = self.msgObject.get(MESSAGEINI_SECTION,BACKGROUNDLENGTH)
  63.             backgroundHeight = self.msgObject.get(MESSAGEINI_SECTION,BACKGROUNDHEIGHT)
  64.             wordSize = self.msgObject.get(MESSAGEINI_SECTION,WORDSIZE)
  65.             wordColor = self.msgObject.get(MESSAGEINI_SECTION,WORDCOLOR)
  66.             isAutoAdjust = int(self.msgObject.get(MESSAGEINI_SECTION,AUTOADJUSET))
  67.             setting = [int(year),int(month),int(day),int(backgroundColor),\
  68.                    int(backgroundLength),int(backgroundHeight),\
  69.                    int(wordSize),int(wordColor),bool(isAutoAdjust)]
  70.             self.settingDialog.getUserDateFromCfgFile(setting)
  71.             self.updateUi(isIni,setting)
  72.         else:
  73.             self.writeIniFile()

  74.     def writeIniFile(self):
  75.         iniFile = ConfigParser.ConfigParser()
  76.         iniFile.read(MESSAGEINIFILENAME)
  77.         if not iniFile.has_section(MESSAGEINI_SECTION):
  78.             iniFile.add_section(MESSAGEINI_SECTION)
  79.         iniFile.set(MESSAGEINI_SECTION,ISINI,1)
  80.         iniFile.set(MESSAGEINI_SECTION,YEAR,2013)
  81.         iniFile.set(MESSAGEINI_SECTION,MONTH,11)
  82.         iniFile.set(MESSAGEINI_SECTION,DAY,22)
  83.         iniFile.set(MESSAGEINI_SECTION,BACKGROUNDCOLOR,6)
  84.         iniFile.set(MESSAGEINI_SECTION,BACKGROUNDLENGTH,115)
  85.         iniFile.set(MESSAGEINI_SECTION,BACKGROUNDHEIGHT,50)
  86.         iniFile.set(MESSAGEINI_SECTION,WORDSIZE,19)
  87.         iniFile.set(MESSAGEINI_SECTION,WORDCOLOR,7)
  88.         iniFile.set(MESSAGEINI_SECTION,AUTOADJUSET,1)
  89.         iniFile.write(open(MESSAGEINIFILENAME,"w"))
  90.         self.iniUi()



  91.     def mouseDoubleClickEvent(self,event):
  92.         if self.settingDialog.exec_():
  93.                 self.settingDialog.show()
  94.         if self.settingDialog.getIsUpdate():
  95.             if self.updateUi(False,self.settingDialog.createSetting()) == 0:
  96.                 if self.settingDialog.exec_():
  97.                     self.settingDialog.show()
  98.                 if self.settingDialog.getIsUpdate():
  99.                     self.updateUi(False,self.settingDialog.createSetting())
  100.             else:
  101.                 return
  102.         else:
  103.             self.iniUi()




  104.     def quit(self):
  105.         self.close()

  106.     def mousePressEvent(self, event):
  107.         if event.button() == QtCore.Qt.LeftButton:
  108.             self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
  109.             event.accept()


  110.     def mouseMoveEvent(self, event):
  111.         if event.buttons() == QtCore.Qt.LeftButton:
  112.             self.move(event.globalPos() - self.dragPosition)
  113.             event.accept()

  114.     def changeUiDirectly(self,setting):
  115.         if self.first is not True:
  116.             self.updateUi(False,setting)
  117.         else:
  118.             return



  119.     def updateUi(self,isIni,setting):
  120.         if setting:
  121.             settedDate = [setting[0],setting[1],setting[2]]
  122.             backgroundColorIndex = setting[3]
  123.             backgroundLength = setting[4]
  124.             backgroundHeight = setting[5]
  125.             wordSize = setting[6]
  126.             wordColorIndex = setting[7]
  127.             if isIni:
  128.                 self.setText(self.tr('请双击我'))
  129.             else:
  130.                 self.setNumber(settedDate)
  131.             self.setBackgroundColor(COLORlIST[backgroundColorIndex])
  132.             self.setWordSize(wordSize)
  133.             self.setWordColor(COLORlIST[wordColorIndex+1])
  134.             if setting[8]:
  135.                 self.autoSetBackgroundSize()
  136.                 self.settingDialog.disableBackgroundSet()
  137.             else:
  138.                 self.resize(int(backgroundLength),int(backgroundHeight))
  139.                 self.settingDialog.enableBackgroundSet()
  140.             return UPDATE_SUCESS
  141.         else:
  142.             return UPDAT_EERROR

  143.     #边框自适应文字大小
  144.     def autoSetBackgroundSize(self):
  145.         self.adjustSize()

  146.     def setNumber(self,dateList):
  147.         date = QtCore.QDate(int(dateList[0]),int(dateList[1]),int(dateList[2]))
  148.         currentDate = QtCore.QDate.currentDate()
  149.         self.number = 0
  150.         if date > currentDate:
  151.             while currentDate != date:
  152.                 currentDate = QtCore.QDate(currentDate).addDays(1)
  153.                 self.number += 1
  154.         else:
  155.             while date != currentDate:
  156.                 date = QtCore.QDate(date).addDays(1)
  157.                 self.number += 1
  158.         self.setText(str(self.number))

  159.     def setBackgroundColor(self,color):
  160.         if color == 'transparent':
  161.             self.setStyleSheet("background:"+str(color))  #这个是设置的线框
  162.         else:
  163.             self.setStyleSheet("background-color:"+str(color))

  164.     def setWordSize(self,size):
  165.         font = self.font()
  166.         font.setPointSize(size)
  167.         font.setBold(True)
  168.         self.setFont(font)

  169.     def setWordColor(self,color):
  170.         palette = QtGui.QPalette()
  171.         palette.setColor(self.foregroundRole(), color)
  172.         self.setPalette(palette)



  173. class SettingDialog(QtGui.QDialog):
  174.     def __init__(self,parent = None):
  175.         super(SettingDialog,self).__init__(parent)
  176.         self.isUpdate = None

  177.         layout = QtGui.QGridLayout()


  178.         '''set the beginning time'''
  179.         timeLabel = QtGui.QLabel(self.tr('设定日期:'))
  180.         yearLabel = QtGui.QLabel(self.tr('年'))
  181.         self.yearCombo = QtGui.QSpinBox()
  182.         self.yearCombo.setRange(1949,2050)


  183.         monthLabel = QtGui.QLabel(self.tr('月'))
  184.         self.monthCombo = QtGui.QSpinBox()
  185.         self.monthCombo.setRange(1,12)


  186.         dayLabel = QtGui.QLabel(self.tr('日'))
  187.         self.dayCombo = QtGui.QSpinBox()
  188.         self.dayCombo.setRange(1,31)

  189.         '''set the colour of background'''
  190.         backgroundColorLabel = QtGui.QLabel(self.tr('边框颜色:'))
  191.         self.backgroundColourCombo = QtGui.QComboBox()
  192.         for color in COLOGRROUP:
  193.             self.backgroundColourCombo.addItem(self.tr(color))

  194.         '''set the number size'''
  195.         self.backgroundLabel = QtGui.QLabel(self.tr('边框大小:'))
  196.         self.backgroungLenthLabel = QtGui.QLabel(self.tr('长:'))
  197.         self.backgroundLengthCombo = QtGui.QSpinBox()
  198.         self.backgroundLengthCombo.setFixedSize(70,20)
  199.         self.backgroundLengthCombo.setRange(40,1500)
  200.         self.backgroundHeightLabel = QtGui.QLabel(self.tr('宽:'))
  201.         self.backgroundHeightCombo = QtGui.QSpinBox()
  202.         self.backgroundHeightCombo.setFixedSize(70,20)
  203.         self.backgroundHeightCombo.setRange(20,500)
  204.         self.autoAdjustCheckBox = QtGui.QCheckBox(self.tr("自适应边框"))


  205.         '''set the size of word'''
  206.         wordSizeLabel = QtGui.QLabel(self.tr('字体大小:'))
  207.         self.wordSizeCombo = QtGui.QSpinBox()
  208.         self.wordSizeCombo.setRange(15,400)


  209.         '''set the color of the word'''
  210.         wordColorLabel = QtGui.QLabel(self.tr('字体颜色:'))
  211.         self.wordColorCombo = QtGui.QComboBox()
  212.         for color in COLOGRROUP[1:]:
  213.             self.wordColorCombo.addItem(self.tr(color))

  214.         #数据发生改变,实时显示在ui上
  215.         self.connect(self.yearCombo, QtCore.SIGNAL("valueChanged(int)"),
  216.                      self.changeUidata)
  217.         self.connect(self.monthCombo, QtCore.SIGNAL("valueChanged(int)"),
  218.                      self.changeUidata)
  219.         self.connect(self.dayCombo, QtCore.SIGNAL("valueChanged(int)"),
  220.                      self.changeUidata)
  221.         self.connect(self.backgroundColourCombo, QtCore.SIGNAL("activated(int)"),
  222.                      self.changeUidata)
  223.         self.connect(self.wordSizeCombo, QtCore.SIGNAL("valueChanged(int)"),
  224.                      self.changeUidata)
  225.         self.connect(self.wordColorCombo, QtCore.SIGNAL("activated(int)"),
  226.                      self.changeUidata)
  227.         self.connect(self.backgroundLengthCombo, QtCore.SIGNAL("valueChanged(int)"),
  228.                      self.changeUidata)
  229.         self.connect(self.backgroundHeightCombo, QtCore.SIGNAL("valueChanged(int)"),
  230.                      self.changeUidata)
  231.         self.connect(self.autoAdjustCheckBox, QtCore.SIGNAL("toggled(bool)"),
  232.                      self.changeUidata)

  233.         #布局
  234.         buttonBox = QtGui.QGridLayout()
  235.         label = QtGui.QLabel()
  236.         okButton = QtGui.QPushButton(self.tr('确定'))
  237.         cancelButton = QtGui.QPushButton(self.tr('取消'))
  238.         closeButton = QtGui.QPushButton(self.tr('关闭'))
  239.         okButton.clicked.connect(self.okReturn)
  240.         cancelButton.clicked.connect(self.cancelAction)
  241.         closeButton.clicked.connect(self.cancelAction)
  242.         buttonBox.addWidget(label,0,0,2,1)
  243.         buttonBox.addWidget(okButton,0,1,1,2)
  244.         buttonBox.addWidget(cancelButton,0,3,1,2)
  245.         buttonBox.addWidget(closeButton,0,5,1,2)

  246.         timeBox = QtGui.QGroupBox(self.tr('时间'))
  247.         timeLayout = QtGui.QGridLayout()
  248.         timeLayout.addWidget(timeLabel,0,0,1,4)
  249.         timeLayout.addWidget(self.yearCombo,0,5,1,2)
  250.         timeLayout.addWidget(yearLabel,0,7,1,1)
  251.         timeLayout.addWidget(self.monthCombo,0,8,1,2)
  252.         timeLayout.addWidget(monthLabel,0,10,1,1)
  253.         timeLayout.addWidget(self.dayCombo,0,11,1,2)
  254.         timeLayout.addWidget(dayLabel,0,13,1,1)
  255.         timeBox.setLayout(timeLayout)

  256.         backgroundBox = QtGui.QGroupBox(self.tr('边框'))
  257.         backgroundLayout = QtGui.QGridLayout()
  258.         backgroundLayout.addWidget(backgroundColorLabel,0,0,1,4)
  259.         backgroundLayout.addWidget(self.backgroundColourCombo,0,5,1,4)

  260.         backgroundLayout.addWidget(self.backgroundLabel,2,0,1,2)
  261.         backgroundLayout.addWidget(self.backgroungLenthLabel,2,2,1,1)
  262.         backgroundLayout.addWidget(self.backgroundLengthCombo,2,3,1,1)
  263.         backgroundLayout.addWidget(self.backgroundHeightLabel,2,4,1,1)
  264.         backgroundLayout.addWidget(self.backgroundHeightCombo,2,5,1,2)
  265.         backgroundLayout.addWidget(self.autoAdjustCheckBox,1,0,1,1)
  266.         backgroundBox.setLayout(backgroundLayout)

  267.         wordBox = QtGui.QGroupBox(self.tr('字体'))
  268.         wordLayout = QtGui.QGridLayout()
  269.         wordLayout.addWidget(wordSizeLabel,0,0,1,1)
  270.         wordLayout.addWidget(self.wordSizeCombo,0,1,1,1)
  271.         wordLayout.addWidget(wordColorLabel,1,0,1,1)
  272.         wordLayout.addWidget(self.wordColorCombo,1,1,1,1)
  273.         wordBox.setLayout(wordLayout)


  274.         layout.addWidget(timeBox)
  275.         layout.addWidget(backgroundBox)
  276.         layout.addWidget(wordBox)
  277.         layout.addLayout(buttonBox,3,0)

  278.         self.setLayout(layout)
  279.         self.setWindowTitle(self.tr('设 置'))

  280.     '''触发的函数'''
  281.     #编写callback来实时的控制ui,将设置的参数传递给ui
  282.     def registerCallback(self,callbackFunc):
  283.         self.updateDirectly = callbackFunc

  284.     def changeUidata(self):
  285.         self.updateDirectly(self.createSetting())

  286.     def disableBackgroundSet(self):
  287.         self.backgroundLabel.setDisabled(True)
  288.         self.backgroungLenthLabel.setDisabled(True)
  289.         self.backgroundLengthCombo.setDisabled(True)
  290.         self.backgroundHeightLabel.setDisabled(True)
  291.         self.backgroundHeightCombo.setDisabled(True)

  292.     def enableDisableBackgroundSet(self):
  293.         if self.autoAdjustCheckBox.isChecked():
  294.             self.disableBackgroundSet()
  295.         else:
  296.             self.enableBackgroundSet()

  297.     def enableBackgroundSet(self):
  298.         self.backgroundLabel.setDisabled(False)
  299.         self.backgroungLenthLabel.setDisabled(False)
  300.         self.backgroundLengthCombo.setDisabled(False)
  301.         self.backgroundHeightLabel.setDisabled(False)
  302.         self.backgroundHeightCombo.setDisabled(False)

  303.     #按下确定键后,保存当前设置的数据,关闭设置窗口
  304.     def okReturn(self):
  305.         self.saveData(self.createSetting())
  306.         self.isUpdate = True
  307.         self.close()

  308.     def cancelAction(self):
  309.         self.isUpdate = False
  310.         self.close()

  311.     def createSetting(self):
  312.         year = self.yearCombo.value()
  313.         month = self.monthCombo.value()
  314.         day = self.dayCombo.value()
  315.         backgroundColor  = self.backgroundColourCombo.currentIndex()
  316.         backgroundLength = self.backgroundLengthCombo.value()
  317.         backgroundHeight = self.backgroundHeightCombo.value()
  318.         wordSize = self.wordSizeCombo.value()
  319.         wordColor = self.wordColorCombo.currentIndex()
  320.         isAutoBackgroundSet = bool(int(self.autoAdjustCheckBox.isChecked()))
  321.         setting = [int(year),int(month),int(day),int(backgroundColor),\
  322.                    int(backgroundLength),int(backgroundHeight),\
  323.                    int(wordSize),int(wordColor),isAutoBackgroundSet]
  324.         return setting

  325.     def getIsAutoAdjust(self,isAuto):
  326.         self.autoAdjustCheckBox.setChecked(isAuto)
  327.         self.enableDisableBackgroundSet()

  328.     def getIsUpdate(self):
  329.         return self.isUpdate

  330.     def getIsReset(self):
  331.         return True

  332.     #保存用户数据,编写配置文件
  333.     def saveData(self,setting):
  334.         cfg_obj = ConfigParser.ConfigParser()
  335.         cfg_obj.read(CFGFILENAME)

  336.         if not cfg_obj.has_section(MESSAGEINI_SECTION):
  337.             cfg_obj.add_section(MESSAGEINI_SECTION)
  338.         cfg_obj.set(MESSAGEINI_SECTION,ISINI,int(0))
  339.         cfg_obj.set(MESSAGEINI_SECTION,YEAR,int(setting[0]))
  340.         cfg_obj.set(MESSAGEINI_SECTION,MONTH,int(setting[1]))
  341.         cfg_obj.set(MESSAGEINI_SECTION,DAY,int(setting[2]))
  342.         cfg_obj.set(MESSAGEINI_SECTION,BACKGROUNDCOLOR,int(setting[3]))
  343.         cfg_obj.set(MESSAGEINI_SECTION,BACKGROUNDLENGTH,int(setting[4]))
  344.         cfg_obj.set(MESSAGEINI_SECTION,BACKGROUNDHEIGHT,int(setting[5]))
  345.         cfg_obj.set(MESSAGEINI_SECTION,WORDSIZE,int(setting[6]))
  346.         cfg_obj.set(MESSAGEINI_SECTION,WORDCOLOR,int(setting[7]))
  347.         cfg_obj.set(MESSAGEINI_SECTION,AUTOADJUSET,int(setting[8]))
  348.         cfg_obj.write(open(CFGFILENAME,"w"))

  349.     #获取初始的用户数据,保证用户所看到的配置与对话框中的数据一致
  350.     def getUserDateFromCfgFile(self,setting):
  351.         self.yearCombo.setValue(int(setting[0]))
  352.         self.monthCombo.setValue(int(setting[1]))
  353.         self.dayCombo.setValue(int(setting[2]))
  354.         self.backgroundColourCombo.setCurrentIndex(int(setting[3]))
  355.         self.backgroundLengthCombo.setValue(int(setting[4]))
  356.         self.backgroundHeightCombo.setValue(int(setting[5]))
  357.         self.wordSizeCombo.setValue(int(setting[6]))
  358.         self.wordColorCombo.setCurrentIndex(int(setting[7]))
  359.         self.getIsAutoAdjust(setting[8])



  360. if __name__ == '__main__':
  361.     app = QtGui.QApplication(sys.argv)
  362.     mainWindow = MainWindow()
  363.     mainWindow.show()
  364.     sys.exit(app.exec_())
复制代码





userManu.zip

75.31 KB, 下载次数: 10

用户手册

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-30 16:42:02 | 显示全部楼层
本帖最后由 小睿webster 于 2014-10-30 19:07 编辑

忘了no picture,you say a jb!上图:

1.PNG
2.PNG
3.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-10-31 18:59:23 | 显示全部楼层
学习了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-29 08:42:05 | 显示全部楼层
学习学习,谢谢分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-22 14:23:16 | 显示全部楼层
学习了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-28 16:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表