这两天看pyqt5了,这个模块可以用来制作桌面软件,自己试着做了个计算器,这里记录一下。
安装:
pip install PyQt5
安装完成继续安装PyQt5-tools
pip install PyQt5-tools
然后配置pycharm
点击:File -》Settings-》Tools -》 External Tools -》点击“+”号
我的配置过了,所以下面显示的有Qt Desi…和pyuic,你配置好之后也会有的。
先设置这三个地方
1,Name:Qt Designer
ps:你自己的designer.exe路径,
2,Programs:D:\ProgramData\Anaconda3\envs\py35\Scripts\designer.exe
ps:$符和ProjectFileDir之间不要有空格,你复制我的可能会有,注意下
3,Working directory: $ ProjectFileDir$
再设置PyUIC,
1,Name:PyUIC
ps:你的python路径
2,Programs:D:\ProgramData\Anaconda3\envs\py35\python.exe
ps:3和4,还是那就话,$符和它包裹的英文字符之间尽量别有空格,我打了空格的,因为不打空格,博客显示不出来
3,Parameters:-m PyQt5.uic.pyuic $ FileName $ -o $ FileNameWithoutExtension $ .py
4,Working directory:$ FileDir $
这段看的不太明白的话可以去网上搜别人的教程,很多的
完成上面的步骤以后,点击 Tools -》External Tools -》Qt Designer 启动我们的Qt Designer
启动后选择:main window,建立空白的窗口,点击 Create
进入之后就是这样的
然后先拖拽16个push button上来,然后双击它就可以编辑输入了
稍微排列就好,大概对其,框与框之间别重叠
然后我们全选这些按钮,就是按住鼠标左键范围选中知道吧,选中之后更改他们的宽高,我四个全写的60
再拉两个push button出来,单击ce,改变最大宽高分别60和180。
清除缓存的宽高还是四个60
然后再次全选,右键,lay out>lay out in a grid
可以上下拉大些
然后再把这俩拖上去
右键空白处,选择lay out>lay out vertically
选中第二个框,改变这俩地方
看下图,选中这个spacer放到显示框和数字框之间
选中它,改这三个地方
再拉一个spacer扔到最顶端,目的与边框保持距离。选中第二个spacer,跟第一个spacer的一样,改那
此时界面就做好了,点击,保存
此时,你保存的文件夹里就会有个ui的文件
选中它,右键,选择pyuic转成python文件
此时文件夹下多了一个py文件
此时在该文件夹下新建一个run.py文件
# -*- coding: utf-8 -*-
# @Author : LiMengDi
# @FILE : run.py
# @Time : 2019/11/27 18:08
# @Software : PyCharm
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from untitled import Ui_MainWindow
class mywindow(QWidget, Ui_MainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.setupUi(self)
#下面是定义界面点击某键时候执行何种操作
def backspace(self):
self.lineEdit.backspace()
def ps_bt(self):
self.lineEdit.insert('1')
def ps_bt2(self):
self.lineEdit.insert('2')
def ps_bt3(self):
self.lineEdit.insert('3')
def ps_bt4(self):
self.lineEdit.insert('4')
def ps_bt5(self):
self.lineEdit.insert('5')
def ps_bt6(self):
self.lineEdit.insert('6')
def ps_bt7(self):
self.lineEdit.insert('7')
def ps_bt8(self):
self.lineEdit.insert('8')
def ps_bt9(self):
self.lineEdit.insert('9')
def ps_bt10(self):
self.lineEdit.insert('0')
def ps_bt11(self):
self.lineEdit.insert('+')
def ps_bt12(self):
self.lineEdit.insert('-')
def ps_bt13(self):
self.lineEdit.insert('*')
def ps_bt14(self):
self.lineEdit.insert('/')
def ps_bt15(self):
self.lineEdit.insert('.')
def ps_bt16(self):
self.lineEdit.insert('=')
self.calculate()
def lineEdit_clear(self):
self.lineEdit.clear()
def calculate(self):
text = self.lineEdit.text()
self.textBrowser.append('%s= %.2f' % (text, eval(text)))
self.lineEdit_clear()
def clear_all(self):
self.textBrowser.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = mywindow()
#撤销
#这句话的意思是,当点击pushButton_17的时候,执行我们上面定义的backspace方法,
#pushButton_17在untitled.py文件里对应的是ce这个键
#所以,这句话的意思就是说当点击ce键的时候,执行backspace这个方法里的操作,也就是删除一个字符
w.pushButton_17.clicked.connect(w.backspace)
#1-0
w.pushButton.clicked.connect(w.ps_bt)
w.pushButton_2.clicked.connect(w.ps_bt2)
w.pushButton_9.clicked.connect(w.ps_bt3)
w.pushButton_10.clicked.connect(w.ps_bt4)
w.pushButton_7.clicked.connect(w.ps_bt5)
w.pushButton_4.clicked.connect(w.ps_bt6)
w.pushButton_3.clicked.connect(w.ps_bt7)
w.pushButton_11.clicked.connect(w.ps_bt8)
w.pushButton_12.clicked.connect(w.ps_bt9)
w.pushButton_6.clicked.connect(w.ps_bt10)
#运算符
w.pushButton_5.clicked.connect(w.ps_bt11)
w.pushButton_8.clicked.connect(w.ps_bt12)
w.pushButton_15.clicked.connect(w.ps_bt13)
w.pushButton_13.clicked.connect(w.ps_bt14)
#点
w.pushButton_14.clicked.connect(w.ps_bt15)
#等
w.pushButton_16.clicked.connect(w.calculate)
#清空缓存
w.pushButton_18.clicked.connect(w.clear_all)
w.show()
sys.exit(app.exec_())
值得注意的是,untitled.py文件里有三个地方需要注释掉,不然会保错
MainWindow.setCentralWidget(self.centralwidget)
MainWindow.setMenuBar(self.menubar)
MainWindow.setStatusBar(self.statusbar)
最后,如果你是跟我一路做过来的,那么我的run.py也是适合你的,你可以直接跑run.py。不行的话,微调一下按钮和执行方法的对应关系就好了最最后,老规矩,看下效果:
经检测,ce(撤销一位数)和清除缓存(删除计算历史记录)也是可用的,大功告成。明天继续看看能不能做出来些别的小程序。
下班喽~~~~~~~~~~~~~~~~~~~~~~~~~~~