一、整理笔记 1.创建一个计算器 from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QGridLayout,QLabel,QLineEdit 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)] # 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) if __name__=='__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) 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() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) 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><!--结束标签-->
|