码迷,mamicode.com
首页 > 其他好文 > 详细

【PyQt5-Qt Designer】简易的数字键盘输入

时间:2019-01-05 23:22:56      阅读:1084      评论:0      收藏:0      [点我收藏+]

标签:lcd   out   fixed   分享   window   gui   src   key   qwidget   

参考如下键盘格式写了一个键盘输入,目前还不能进行运算,后期完善。。。

技术分享图片

效果如下:

技术分享图片

完整代码:

from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QLCDNumber,QFormLayout,QLineEdit,QLabel,QGridLayout)
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt,QTimer,QDateTime,QDate,QTime,QSize
import sys

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,330,310)
        self.setWindowTitle("数字键盘输入")
        gridLayout = QGridLayout()
        self.display = QLineEdit("0")
        self.display.setFixedSize(QSize(287,40))
        self.display.setReadOnly(True)
        self.display.setAlignment(Qt.AlignRight)
        self.display.setFont(QFont("微软雅黑",14,QFont.Bold))
        gridLayout.addWidget(self.display,0,0,1,4)
        self.showNum = ""

        keys = [Clear, /, *, -,
                7, 8, 9, +,
                4, 5, 6, ‘‘,
                1, 2, 3, Enter,
                0, ‘‘, ., ‘‘]
        position = [(0, 0), (0, 1), (0, 2), (0, 3),
                    (1, 0), (1, 1), (1, 2), (1, 3),
                    (2, 0), (2, 1), (2, 2), (2, 2),
                    (3, 0), (3, 1), (3, 2), (3, 3),
                    (4, 0), (4, 1), (4, 2), (4, 3), ]
        for item in range(len(keys)):
            btn = QPushButton(keys[item])
            btn.setFixedSize(QSize(60, 40))
            btn.setFont(QFont("微软雅黑",12,QFont.Bold))
            btn.clicked.connect(self.btnClicked)
            if keys[item] == "+":
                gridLayout.addWidget(btn, 2, 3, 2, 1)
                btn.setFixedSize(QSize(60, 90))
            elif keys[item] == "Enter":
                gridLayout.addWidget(btn, 4, 3, 2, 1)
                btn.setFixedSize(QSize(60, 90))
            elif keys[item] == "0":
                gridLayout.addWidget(btn, 5, 0, 1, 2)
                btn.setFixedSize(QSize(142, 40))
            elif keys[item] == "":
                continue
            else:
                gridLayout.addWidget(btn, position[item][0]+1, position[item][1], 1, 1)
        self.setLayout(gridLayout)

    def btnClicked(self):
        sender = self.sender()
        symbols = ["Clear","/","*","-","+","Enter"]
        if sender.text() not in symbols:
            self.showNum += sender.text()
            self.display.setText(self.showNum)
        elif sender.text() == "Clear":
            self.display.setText("0")
            self.showNum = ""

if __name__ == __main__:
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

 

【PyQt5-Qt Designer】简易的数字键盘输入

标签:lcd   out   fixed   分享   window   gui   src   key   qwidget   

原文地址:https://www.cnblogs.com/XJT2018/p/10226695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!