我的账户
啄木鸟学院

专注软件测试菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

python_武浩杰_20210127

[复制链接]
海底月是天上月学员认证 发表于 2021-1-27 20:55:23 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
本帖最后由 海底月是天上月 于 2021-1-27 20:55 编辑

1.创建一个计算器
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QGridLayout,QLabel,QLineEdit
from PyQt5.QtCore import Qt,QCoreApplication
import sys
    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)]
        # print(positions)
        #创建按钮并添加到表格中
        for label,position in zip(labels,positions):
            btn = QPushButton(label)
            btn.clicked.connect(self.function)
            grid.addWidget(btn,*position)
            # grid.addWidget(btn,position[0],position[1])
        self.move(500,500)
        self.setWindowTitle('计算器')
        self.show()

    def function(self):
        #获取按键的内容
        text = self.sender().text() #sender发送者
        # print(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)
        # self.screen.setText(self.screen.text()+text)

2.创建一个栏目
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,200)
        self.setWindowTitle('程序菜单')
        self.show()
3.HTML
<!DOCTYPE html>
<!-- 标签, -->
<html><!--开始标签,父标签,标签名   根标签  双标签元素-->
    <!--标签体-->
    <head><!--子标签-->
        <!--head中定义页面的样式以及动态效果-->
        <!--charset="utf-8":标签的属性,简称属性      meta:源数据-->
        <meta charset="utf-8"><!--单标签元素-->
        <title>这是我的第一个网页</title>
        <style>
            /*CSS Cascding Style Sheet层叠样式*/
            h1 {
                color:darkturquoise
            }
            .aaa {
                color:dodgerblue;
                font-size:xx-large;
            }
            p {
                font-family: '仿宋';
            }

        </style>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <!--body中是页面所展示的内容-->
        <h1>中国</h1>
        <p class='aaa italic'> 中国马上就要建设空间站</p>
    </body>
</html><!--结束标签-->


回复

使用道具 举报

关注0

粉丝0

帖子30

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

客服电话:17792550360

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

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

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