麦田孤望者 发表于 2020-2-22 16:43

Pyqt5初步(1)——一个计算器

弄了个Pyqt5 也是python GUI的一种
不得不说功能是真的强大

还有 为了每次新建一个项目不那么麻烦,我还写了一个create.py也贴在这里吧 放在最后 把创建项目重复的部分都自动创建了
这是个计算器
emmm本来是昨天写完的但是太晚了就没发出来...而且忘加小数点了
我是用的Qt Designer来布置
创建的是widdget

main.py 内容
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from functools import partial
import demo03


class Math_():
    def __init__(self):
      self.txt=''
      self.a=0
      #ui.lcdNumber_out.setDigitCount(5)
   
    def container(self,num):
      self.txt += str(num)
      print('\r'+self.txt,flush=True,end='')
      ui.output.setText(self.txt)
    def count(self):
      #print('self.a='+self.txt)
      a =eval(self.txt)
      #$print(a)
      ui.lcdNumber_out.display(a)
      self.txt = str(a)
      ui.output.setText(str(a))
    def zero(self,ui):
      self.container('0')
    def one(self,ui):
      self.container('1')
    def two(self,ui):
      self.container('2')
    def three(self,ui):
      self.container('3')
    def four(self,ui):
      self.container('4')
    def five(self,ui):
      self.container('5')
    def six(self,ui):
      self.container('6')
    def seven(self,ui):
      self.container('7')
    def eight(self,ui):
      self.container('8')
    def nine(self,ui):
      self.container('9')
    def jia(self,ui):
      self.container('+')
    def jian(self,ui):
      self.container('-')
    def cheng(self,ui):
      self.container('*')
    def chu(self,ui):
      self.container('/')
    def dengyu(self,ui):
      self.count()
    def ce(self,ui):
      ui.output.setText('')
      self.txt=''
      print('')
      ui.lcdNumber_out.display(0)
   
   
b = Math_()
   

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = demo03.Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    ui.pushButton_0.clicked.connect(partial(b.zero, ui))
    ui.pushButton_1.clicked.connect(partial(b.one, ui))
    ui.pushButton_2.clicked.connect(partial(b.two, ui))
    ui.pushButton_3.clicked.connect(partial(b.three, ui))
    ui.pushButton_4.clicked.connect(partial(b.four, ui))
    ui.pushButton_5.clicked.connect(partial(b.five, ui))
    ui.pushButton_6.clicked.connect(partial(b.six, ui))
    ui.pushButton_7.clicked.connect(partial(b.seven, ui))
    ui.pushButton_8.clicked.connect(partial(b.eight, ui))
    ui.pushButton_9.clicked.connect(partial(b.nine, ui))
    ui.pushButton_jia.clicked.connect(partial(b.jia, ui))
    ui.pushButton_jian.clicked.connect(partial(b.jian, ui))
    ui.pushButton_cheng.clicked.connect(partial(b.cheng, ui))
    ui.pushButton_chu.clicked.connect(partial(b.chu, ui))
    ui.pushButton_dengyu.clicked.connect(partial(b.dengyu, ui))
    ui.pushButton_ce.clicked.connect(partial(b.ce, ui))
    MainWindow.setFixedSize(MainWindow.width(), MainWindow.height())

    sys.exit(app.exec_())
   


demo03.py 内容
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'demo03.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
      MainWindow.setObjectName("MainWindow")
      MainWindow.resize(271, 308)
      self.pushButton_5 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_5.setGeometry(QtCore.QRect(61, 207, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_5.setFont(font)
      self.pushButton_5.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_5.setObjectName("pushButton_5")
      self.frame = QtWidgets.QFrame(MainWindow)
      self.frame.setGeometry(QtCore.QRect(9, 9, 253, 94))
      self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
      self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
      self.frame.setObjectName("frame")
      self.lcdNumber_out = QtWidgets.QLCDNumber(self.frame)
      self.lcdNumber_out.setEnabled(True)
      self.lcdNumber_out.setGeometry(QtCore.QRect(0, 0, 251, 51))
      font = QtGui.QFont()
      font.setPointSize(11)
      font.setBold(True)
      font.setWeight(75)
      self.lcdNumber_out.setFont(font)
      self.lcdNumber_out.setDigitCount(15)
      self.lcdNumber_out.setProperty("intValue", 0)
      self.lcdNumber_out.setObjectName("lcdNumber_out")
      self.output = QtWidgets.QTextBrowser(self.frame)
      self.output.setEnabled(True)
      self.output.setGeometry(QtCore.QRect(0, 60, 251, 31))
      font = QtGui.QFont()
      font.setPointSize(15)
      self.output.setFont(font)
      self.output.setObjectName("output")
      self.pushButton_cheng = QtWidgets.QPushButton(MainWindow)
      self.pushButton_cheng.setGeometry(QtCore.QRect(165, 256, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_cheng.setFont(font)
      self.pushButton_cheng.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_cheng.setObjectName("pushButton_cheng")
      self.pushButton_9 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_9.setGeometry(QtCore.QRect(113, 158, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_9.setFont(font)
      self.pushButton_9.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_9.setObjectName("pushButton_9")
      self.pushButton_8 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_8.setGeometry(QtCore.QRect(61, 158, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_8.setFont(font)
      self.pushButton_8.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_8.setObjectName("pushButton_8")
      self.pushButton_4 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_4.setGeometry(QtCore.QRect(9, 207, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_4.setFont(font)
      self.pushButton_4.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_4.setObjectName("pushButton_4")
      self.pushButton_jian = QtWidgets.QPushButton(MainWindow)
      self.pushButton_jian.setGeometry(QtCore.QRect(217, 207, 45, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_jian.setFont(font)
      self.pushButton_jian.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_jian.setObjectName("pushButton_jian")
      self.pushButton_6 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_6.setGeometry(QtCore.QRect(113, 207, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_6.setFont(font)
      self.pushButton_6.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_6.setObjectName("pushButton_6")
      self.pushButton_jia = QtWidgets.QPushButton(MainWindow)
      self.pushButton_jia.setGeometry(QtCore.QRect(165, 207, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_jia.setFont(font)
      self.pushButton_jia.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_jia.setObjectName("pushButton_jia")
      self.pushButton_ce = QtWidgets.QPushButton(MainWindow)
      self.pushButton_ce.setGeometry(QtCore.QRect(9, 109, 251, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_ce.setFont(font)
      self.pushButton_ce.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_ce.setObjectName("pushButton_ce")
      self.pushButton_0 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_0.setGeometry(QtCore.QRect(165, 158, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_0.setFont(font)
      self.pushButton_0.setObjectName("pushButton_0")
      self.pushButton_7 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_7.setGeometry(QtCore.QRect(9, 158, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_7.setFont(font)
      self.pushButton_7.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_7.setObjectName("pushButton_7")
      self.pushButton_2 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_2.setGeometry(QtCore.QRect(61, 256, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_2.setFont(font)
      self.pushButton_2.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_2.setObjectName("pushButton_2")
      self.pushButton_dengyu = QtWidgets.QPushButton(MainWindow)
      self.pushButton_dengyu.setGeometry(QtCore.QRect(217, 158, 45, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_dengyu.setFont(font)
      self.pushButton_dengyu.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_dengyu.setObjectName("pushButton_dengyu")
      self.pushButton_chu = QtWidgets.QPushButton(MainWindow)
      self.pushButton_chu.setGeometry(QtCore.QRect(217, 256, 45, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_chu.setFont(font)
      self.pushButton_chu.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_chu.setObjectName("pushButton_chu")
      self.pushButton_1 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_1.setGeometry(QtCore.QRect(9, 256, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_1.setFont(font)
      self.pushButton_1.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_1.setObjectName("pushButton_1")
      self.pushButton_3 = QtWidgets.QPushButton(MainWindow)
      self.pushButton_3.setGeometry(QtCore.QRect(113, 256, 46, 43))
      font = QtGui.QFont()
      font.setPointSize(26)
      self.pushButton_3.setFont(font)
      self.pushButton_3.setIconSize(QtCore.QSize(19, 19))
      self.pushButton_3.setObjectName("pushButton_3")

      self.retranslateUi(MainWindow)
      QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
      _translate = QtCore.QCoreApplication.translate
      MainWindow.setWindowTitle(_translate("MainWindow", "Calculator"))
      self.pushButton_5.setText(_translate("MainWindow", "5"))
      self.pushButton_cheng.setText(_translate("MainWindow", "*"))
      self.pushButton_9.setText(_translate("MainWindow", "9"))
      self.pushButton_8.setText(_translate("MainWindow", "8"))
      self.pushButton_4.setText(_translate("MainWindow", "4"))
      self.pushButton_jian.setText(_translate("MainWindow", "-"))
      self.pushButton_6.setText(_translate("MainWindow", "6"))
      self.pushButton_jia.setText(_translate("MainWindow", "+"))
      self.pushButton_ce.setText(_translate("MainWindow", "CE"))
      self.pushButton_0.setText(_translate("MainWindow", "0"))
      self.pushButton_7.setText(_translate("MainWindow", "7"))
      self.pushButton_2.setText(_translate("MainWindow", "2"))
      self.pushButton_dengyu.setText(_translate("MainWindow", "="))
      self.pushButton_chu.setText(_translate("MainWindow", "/"))
      self.pushButton_1.setText(_translate("MainWindow", "1"))
      self.pushButton_3.setText(_translate("MainWindow", "3"))

create.py 内容
import os,sys

print('请确保您的.ui文件名与file name相同')
name = input("Input folder name here:")#文件夹名
name2 = input("Input file name here:")#文件名

name3 = input("*.py or *.pyw? Input 'py' or 'pyw' here:")
while name3!='py' and name3!='pyw':#是否需要命令行
    name3 = input("*.py or *.pyw? Input 'py' or 'pyw' here:")

path_now = os.path.dirname(os.path.realpath(__file__))
path_new = path_now+"\\"+name

try:
    os.makedirs(path_new)
except FileExistsError:
    pass


txt = '''import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

import {}

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = {}.Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
    '''.format(name2,name2)

with open(path_new+'\\'+'main.py','w') as f:
    f.write(txt)

with open(path_new+'\\'+'click_me.bat','w') as f:
    f.write('pyuic5 -o {}.py {}.ui'.format(name2,name2))


麦田孤望者 发表于 2020-2-22 16:46

然后那个create.py就是双击之后回答三个问题 之后自动创建文件夹和两个文件 用Qt designer的话把.ui文件保存到这个文件夹里(.ui文件名要和你回答的第二个问题一样)
然后双击click_me.bat,等编译完成再双击main.py就可以运行了

ciker_li 发表于 2020-2-22 16:52

python就是GUI麻烦

xQQ 发表于 2020-2-22 18:55

不错,学习了。 Pyqt

mingrenlele 发表于 2020-2-22 19:18

看界面怎么像Python,学习了。

wikiyc 发表于 2020-2-23 18:58

学习了,谢谢分享。

在线苦等 发表于 2020-2-24 20:40

thank you for your resources !

打野不清兵 发表于 2021-4-7 14:27

棒棒棒~~~

zsw555 发表于 2021-4-9 18:31

下来,学习
页: [1]
查看完整版本: Pyqt5初步(1)——一个计算器