我的账户
啄木鸟学院

专注软件测试菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

python-贺威栋-2021.01.07

[复制链接]
I5029学员认证 发表于 2021-1-27 20:21:15 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
'''表格布局'''
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QGridLayout,QLabel,QLineEdi
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)
        # 创建所有按钮的标签
        labels = ['(*^▽^*)','CE','C','Close',
                    '7','8','9','/',
                    '4','5','6','*',
                    '1','2','3','-',
        # 创建按钮的位置参数
        positions = [(x,y) for x in range(1,6) for y in range(1,5)]
        # 创建按钮并添加到表格中
        for label,position in zip(labels,positions):
            btn = QPushButton(label)
            btn.clicked.connect(self.function)
            grid.addWidget(btn,*position)
        self.move(500,500)
        self.setWindowTitle('计算器')
        self.show()
    def function(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 calculate(self):
        '''计算结果'''
        content = self.screen.text()
        result = eval(content)
        self.screen.setText(str(result))
    def clear(self):
        '''清除'''
        self.screen.setText('')
    def close(self):
        '''关闭计算器'''
        QCoreApplication.quit()
    def write(self,text):
        '''向显示器输入内容'''
        content = self.screen.text()
        if len(content) == 0:
            content = text
        elif text in '+-*/' and content[-1] 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('标题:')
        author = QLabel('作者:')
        content = QLabel('正文:')
        title_edit = QLineEdit()
        author_edit = QLineEdit()
        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,300)
        self.setWindowTitle('文本编辑器')
        self.show()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
from PyQt5.QtWidgets import QApplication,QMainWindow
import sys,time
class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        # 创建状态栏
        status = self.statusBar()
        # 在状态栏显示信息
        status.showMessage("准备好了!")
        # time.sleep(2)
        # status.showMessage("可以开始了!")
        self.setGeometry(300,300,300,300)
        self.setWindowTitle("状态栏")
        self.show()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
from PyQt5.QtWidgets import QApplication,QMainWindow,qApp,QAction
from PyQt5.QtGui import QIcon
import sys
class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        # 创建一个菜单条目
        exitAction = QAction(QIcon('python\day09\exit3.png'),'退出',self)
        # 添加快捷键
        exitAction.setShortcut('ctrl+q')
        # 添加一个提示
        exitAction.setStatusTip('退出程序')
        # 给菜单条目添加动作
        exitAction.triggered.connect(qApp.quit)
        # 创建一个菜单栏
        menuBar = self.menuBar()
        # 添加一个File菜单
        fileMenu = menuBar.addMenu('File')
        # 添加菜单条目
        fileMenu.addAction(exitAction)
        self.setGeometry(300,300,300,300)
        self.setWindowTitle('程序菜单')
        self.show()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
from PyQt5.QtWidgets import QApplication,QMainWindow,qApp,QAction
from PyQt5.QtGui import QIcon
import sys
class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        exitAction = QAction(QIcon('python\day09\exit3.png'),'退出',self)
        exitAction.setShortcut('ctrl+q')
        exitAction.triggered.connect(qApp.quit)

回复

使用道具 举报

关注0

粉丝0

帖子27

发布主题
大家都在学
课堂讨论
一周热帖排行最近7x24小时热帖
关注我们
专注软件测试菁英教育

客服电话:17792550360

客服时间:9:00-21:00

卓目鸟学苑 - 专注软件测试菁英教育!( 陕ICP备20001493号-1 )

版权所有 © 西安菁英教育科技有限公司 2023-2026