菁英科技(卓目鸟学苑)- 专注软件测试菁英教育

标题: python-张荣亮-2021.01.27 [打印本页]

作者: 淰7331    时间: 2021-1-27 20:14
标题: python-张荣亮-2021.01.27
'''表格布局'''
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QGridLayout,QLabel,QLineEdit,QTextEdit
from PyQt5.QtCore import Qt,QCoreApplication
import sys
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        # 创建一个表格布局
        grid = QGridLayout()
        self.setLayout(grid)
        # 创建计算机的显示器
        self.screen = QLineEdit()
        # 设置显示器为只读模式
        self.screen.setReadOnly(True)
        # 设置右对齐
        self.screen.setAlignment(Qt.AlignRight)
        grid.addWidget(self.screen,0,1,1,4)
        # self.setGeometry(300,300,300,250)
        # self.setWindowTitle('文本编辑器')
        # self.show()
        # 创建所有按钮的标签
        labels = ['<——','CE','C','Close',
                    '7','8','9','/',
                    '4','5','6','*',
                    '1','2','3','-',
                    '0','.','=','+',
        # 创建按钮的位置参数
        positions = [(x,y) for x in range(1,6) for y in range(1,5)]
        # print(positions)
        # 创建按钮并添加到表格中
        for label,position in zip(labels,positions):
            btn = QPushButton(label)
            btn.clicked.connect(self.funcition)
            # grid.addWidget(btn,position[0],position[1])
            grid.addWidget(btn,*position)
            # backspace = QPushButton('<——')
            # grid.addWidget(backspace,1,1)
        self.move(500,500)
        self.setWindowTitle('计算器')
        self.show()
    def funcition(self):
        # 获取按键的内容
        text = self.sender().text()
        # 判断被按下的按钮,然后执行相应的操作
        if text == '<——':
            self.backspace()
        elif text == 'C' or text == 'CE':
            self.clear()
        elif text == 'Close':
            self.close()
        elif text == '=':
            self.calculate()
        else:
            self.write(text)   
    def backspace(self):
        '''退格'''
        # 获取文本框的内容
        content = self.screen.text()
        # 向文本框写入内容
        self.screen.setText(content[:-1])
    def clear(self):
        '''清除显示器内容'''
        self.screen.setText('')
    def close(self):
        '''关闭计算器'''
        QCoreApplication.quit()
    def calculate(self):
        '''计算结果'''
        content = self.screen.text()
        result = eval(content)
        self.screen.setText(str(result))
    def write(self,text):
        '''向显示器写入内容'''
        content = self.screen.text()
        if len(content) == 0:
            content = text
        elif content[-1] in '+-*/' and text in '+-*/':
            content = content[:-1]+text
        else:
            content = content+text   
        self.screen.setText(content)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

from PyQt5.QtWidgets import QApplication,QWidget,QLabel,QLineEdit,QTextEdit,QGridLayout
import sys
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        grid = QGridLayout()
        self.setLayout(grid)
        title = QLabel('标题:')
        title_edit = QLineEdit()
        author = QLabel('作者:')
        author_edit = QLineEdit()
        content = QLabel('正文:')
        content_edit = QTextEdit()
        grid.addWidget(title,1,1)
        grid.addWidget(title_edit,1,2)
        grid.addWidget(author,2,1)
        grid.addWidget(author_edit,2,2)
        grid.addWidget(content,3,1)
        grid.addWidget(content_edit,3,2)
        self.setGeometry(300,300,300,250)
        self.setWindowTitle('文本编辑器')
        self.show()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())






欢迎光临 菁英科技(卓目鸟学苑)- 专注软件测试菁英教育 (http://www.zmnxy.com/) Powered by Discuz! X3.4