• Pyqt5_实例1


      1 #coding=utf-8
      2 '''
      3 Created on 2018年11月2日
      4 
      5 @author: yanerfree
      6 '''
      7 
      8 import sys
      9 from PyQt5.QtWidgets import *
     10 from PyQt5.QtCore import *
     11 from PyQt5.QtGui import *
     12 from future.types.newint import long
     13 
     14 #测试调试输出开关,正式发布需调整为False
     15 mytest = True
     16 mytest = False
     17 
     18 class WindowDemo(QWidget):
     19     def __init__(self):
     20         super().__init__()
     21         self.initUI()
     22         
     23     def initUI(self):
     24         glayout = QGridLayout()
     25         glayout.setSpacing(10)
     26         
     27         lable1 = QLabel("请输入第 一 个序列号 :")
     28         lable2 = QLabel("需要生成的序列号个数 : ")
     29         lable3 = QLabel("生 成 的 序 列 号  :")
     30         #lable3.setAlignment(Qt.AlignCenter)
     31         
     32         self.lineEdit1 = QLineEdit()
     33         self.lineEdit2 = QLineEdit()
     34         self.textEdit = QTextEdit()
     35         
     36         self.btn1 = QPushButton("开始")
     37         self.btn1.clicked.connect(self.start)
     38         
     39         glayout.addWidget(lable1,1,1)
     40         glayout.addWidget(lable2,2,1)
     41         glayout.addWidget(lable3,3,1)
     42         glayout.addWidget(self.btn1,9,2)
     43         
     44         glayout.addWidget(self.lineEdit1,1,2,1,3)
     45         glayout.addWidget(self.lineEdit2,2,2,1,3)
     46         glayout.addWidget(self.textEdit,3,2,5,3)
     47         
     48         
     49         '''
     50         fromlayout.addRow(labl1, self.lineEdit1)
     51         fromlayout.addRow(labl2, self.lineEdit2)
     52         fromlayout.addRow(labl3, self.textEdit)
     53         fromlayout.addWidget(self.btn1)
     54         '''
     55         
     56 
     57         self.setLayout(glayout)   
     58         self.setWindowTitle("GenerateSerialNum") 
     59         #self.resize(400, 100) 
     60         self.setGeometry(100,60,500,500) 
     61         #self.setWindowIcon(QIcon(r"../photo/pikachu.png"))#设置窗体图标
     62         
     63         
     64     def getparam(self):
     65         #print("get params")
     66         self.param1 = self.lineEdit1.text()
     67         self.param2 = self.lineEdit2.text()
     68 
     69     def start(self):
     70         self.btn1.setCheckable(False)
     71         self.getparam()
     72         self.left = ""
     73         self.right = ""
     74         index = self.getNumindex()
     75         if index == -1:
     76             return 
     77         if mytest: print("获取的index:",index)#ASDCXSW12345678000
     78         self.left = str(self.param1)[0:index]
     79         self.right = str(self.param1)[index:]
     80         if mytest: print("self.left:",self.left)
     81         if mytest: print("self.right:",self.right)
     82         numlist = self.generateNum()
     83         if numlist == -1:
     84             return 
     85         content=""
     86         for item in numlist:
     87             content +=  str(item)
     88             
     89         self.textEdit.setPlainText(content)
     90         self.btn1.setCheckable(True)
     91      
     92     def generateNum(self):
     93         numlist = []
     94         if len(self.param2)==0:
     95             if mytest: print("请输入需要生成的序列号个数")
     96             self.showmsg("warning","请输入需要生成的序列号个数")
     97             return -1
     98         if not str(self.param2).isdigit():
     99             if mytest: print("请正确输入需要生成的序列号个数,必须是整数")
    100             self.showmsg("warning","请正确输入需要生成的序列号个数,必须是整数")
    101             return -1
    102         if mytest: print("self.parm2:",int(self.param2))
    103         tmp = 0
    104         for i in range(int(self.param2)):
    105             tmp = long(self.right) + i
    106             #print("tmp:",tmp)
    107             #tmp ="%s%d"%(self.left,tmp)
    108             tmp =self.left + str(tmp) + "
    "
    109             numlist.append(tmp)
    110             
    111         return numlist   
    112         
    113     def getNumindex(self):
    114         l = len(self.param1)
    115         if mytest: print("输入的第一个序列号长度为:",l)
    116         if not str(self.param1).isalnum():#必须是数字和字母的混合
    117             if mytest: print("输入的序列号不正确,请重新输入")
    118             self.showmsg("warning","输入的序列号不正确,请重新输入")
    119             return -1
    120         if str(self.param1).isalpha():#如果全是字母
    121             if mytest: print("输入的序列号全是字母,请重新输入")
    122             self.showmsg("warning","输入的序列号全是字母,请重新输入")
    123             return -1
    124         if str(self.param1)[-1].isalpha():#如果是字母
    125             if mytest: print(str(self.param1)[-1],"输入的序列号最后一位必须是数字,请重新输入")
    126             self.showmsg("warning","输入的序列号最后一位必须是数字,请重新输入")
    127             return -1
    128         index = 0
    129         for i in range(l):
    130             if self.param1[i].isalpha():
    131                 if mytest: print(self.param1[i],"是字母")
    132                 #self.left = self.left + self.param1[i]
    133             else:
    134                 if mytest: print(self.param1[i],"是数字")
    135                 if self.param1[i:].isdigit():#判断是否数字
    136                     if mytest: print(self.param1[i:],"全是数字")
    137                     index =i
    138                     break
    139                     #self.right = self.param1[i]
    140                 else:
    141                     #self.left = self.left + self.param1[i]
    142                     if mytest: print(self.param1[i:],"不全是数字")
    143                     index += 1
    144         return (index)    
    145     
    146       
    147     def showmsg(self,t,msg): 
    148         if(t == "warning"):
    149             # QMessageBox.warning(self,"Warining","没有需要保存的内容",QMessageBox.Ok)
    150             QMessageBox.warning(self,"Warining",msg,QMessageBox.Ok)
    151         if(t == "info"):
    152             QMessageBox.information(self,"info",msg,QMessageBox.Yes,QMessageBox.Yes)
    153 
    154 
    155 if __name__ == '__main__':
    156     # 每一个pyqt程序中都需要有一个QApplication对象,sys.argv是一个命令行参数列表
    157     app = QApplication(sys.argv)
    158     #实例化窗口
    159     demo = WindowDemo()
    160     #显示
    161     demo.show()
    162     #进入程序的主循环,遇到退出情况,终止程序
    163     sys.exit(app.exec_())        

    打包成可执行程序

    pip install PyInstaller,安装PyInstaller

    pyinstaller -F -w F:exercise20181025GenerateSerialNum.py

  • 相关阅读:
    js开发笔记
    安全相关开发笔记
    常用.NET库使用总结
    Windows使用总结
    .NET Web开发笔记
    Unity插件使用总结
    WinForm开发笔记
    C#开发笔记
    iTunes使用总结
    Mac使用总结
  • 原文地址:https://www.cnblogs.com/yaner2018/p/9923366.html
Copyright © 2020-2023  润新知