PyQt5快速入门(三)PyQt5基本窗口组件
QMainWindow、QWidget、QDialog用于创建窗口,可以直接使用,也可以派生使用。
QMainWindow窗口包含菜单栏、工具栏、状态栏、标题栏等,是最常见的窗口形式。
QDialog是对话框窗口的基类,主要用于执行短期任务,或与用户进行交互,可以是模态或非模态的。QDialog对话框没有菜单栏、工具栏、状态栏等。
QWidget是Qt图形组件的基类,可以作为顶层窗口,也可以嵌入到其它组件中。
QMainWindow是顶层窗口,QMainWindow有自己的布局管理器,不能使用setLayout对其进行设置,布局如下:
Menu Bar是菜单栏,Toolbars是工具栏,Dock Widgets是停靠窗口,Central Widget是中央窗口,Status Bar是状态栏。
import sysfrom PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QDesktopWidget, QToolBarclass MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) # 菜单栏设置 self.menuBar = self.menuBar() self.menuBar.addAction("File") self.menuBar.addAction("View") # 工具栏设置 open = QToolBar() open.addAction("OPen") self.addToolBar(open) close = QToolBar() close.addAction("Close") self.addToolBar(close) # 中央组件设置 self.window = QWidget() self.setCentralWidget(self.window) # 状态栏设置 self.statusBar = self.statusBar() self.statusBar.showMessage("This is an status message.", 5000) label = QLabel("permanent status") self.statusBar.addPermanentWidget(label) self.resize(800, 600) self.setWindowTitle("MainWindow Demo") self.center() def center(self): screen = QDesktopWidget().screenGeometry() size = self.geometry() self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
二、QWidget1、QWidget简介
QWidget是所有GUI界面组件的基类,所有窗口和控件都直接或间接继承自QWidget基类。
2、窗口坐标系统Qt使用统一的坐标系统来定位窗口控件的位置和大小,坐标系统如下:
以屏幕的左上角为原点,即(0,0),从左向右为X轴正向,从上向下为Y轴正向,屏幕的坐标系统用于定位顶层窗口。窗口内部有自己的坐标系统,窗口坐标系统以左上角为原点,即(0,0),从左向右为X轴正向,从上向下为Y轴正向,原点、X轴、Y轴围成的区域为客户区,在客户区的周围是标题栏、边框。
intx() const;
inty() const;
int width() const;
int height() const;
x()、y()获取窗口左上角的坐标,但width()和height()获取的是客户区的宽和高。
geometry().x()、geometry().y()获取客户区的左上角坐标,geometry().width()、geometry().height()获取客户区的宽度和高度。
frameGeometry().x()、frameGeometry().y()获取客户区的左上角坐标,frameGeometry().width()、frameGeometry().height()获取包含客户区、标题栏、边框在内的宽度和高度。
import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDesktopWidget, QToolBarclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("MainWidget") self.resize(800, 600) self.center() def center(self): screen = QDesktopWidget().screenGeometry() size = self.geometry() self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) def dispalyGeometry(self): x = self.x() y = self.y() print("x: {0}, y: {1}".format(x, y)) x = self.pos().x() y = self.pos().y() print("x: {0}, y: {1}".format(x, y)) x = self.frameGeometry().x() y = self.frameGeometry().y() print("x: {0}, y: {1}".format(x, y)) x = self.geometry().x() y = self.geometry().y() print("x: {0}, y: {1}".format(x, y)) print("geometry: ", self.geometry()) print("frameGemetry: ", self.frameGeometry())if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.show() window.dispalyGeometry() sys.exit(app.exec_())
三、QLabel1、QLabel简介
QLabel作为一个占位符可以显示不可编辑的文本、图片、GIF动画,QLabel是界面的标签类,继承自QFrame。
2、QLabel实例import sysfrom PyQt5.QtWidgets import QApplication, QLabelfrom PyQt5.QtCore import Qtfrom PyQt5.QtGui import QPaletteif __name__ == "__main__": app = QApplication(sys.argv) window = QLabel() window.setWindowTitle("QLabel Demo") window.setText("www.baidu.com") window.setOpenExternalLinks(True) pallette = QPalette() pallette.setColor(QPalette.Window, Qt.blue) window.setPalette(pallette) window.setAlignment(Qt.AlignCenter) window.setAutoFillBackground(True) window.resize(400, 200) window.show() sys.exit(app.exec_())
四、文本框控件1、QLineEdit
QLineEdit是单行文本框控件,可以编辑单行字符串,用于接收用户输入。
import sysfrom PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QLabelfrom PyQt5.QtCore import Qtfrom PyQt5.QtGui import QIntValidatorclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) label = QLabel("input: ", self) label.move(0, 0) lineEdit = QLineEdit(self) # 设置验证器 intValidator = QIntValidator() lineEdit.setValidator(intValidator) lineEdit.setMaxLength(10) lineEdit.move(50, 0) lineEdit.textChanged.connect(self.displayText) def displayText(self, text): print(text)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
2、QTextEdit
QTextEdit是一个多行文本框编辑控件,可以显示、编辑多行文本编辑内容,当文本内容超出控件显示范围时,可以显示水平和垂直滚动条,QTextEdit不仅可以显示文本,还可以显示HTML文档。
import sysfrom PyQt5.QtWidgets import QApplication, QTextEdit, QWidget, QLabelfrom PyQt5.QtCore import Qtfrom PyQt5.QtGui import QIntValidatorclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) label = QLabel("input: ", self) label.move(0, 0) self.textEdit = QTextEdit(self) self.textEdit.move(50, 0) self.textEdit.setPlainText("Hello, PyQt5") self.textEdit.textChanged.connect(self.displayText) def displayText(self): print(self.textEdit.toPlainText())if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
五、按钮控件1、QPushButton
QPushButton继承自QAbstractButton,形状为长方形,文本、图标显示在长方形区域。
import sysfrom PyQt5.QtWidgets import QApplication, QPushButton, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button1 = QPushButton("OK", self) button1.clicked.connect(lambda: self.onClicked(button1)) def onClicked(self, button): print("Button {0} is clicked.".format(button.text()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
2、QRadioButton
QRadioButton继承自QAbstractButton,提供了一组可选的按钮和标签,用户可以选择其中一个选项,标签用于显示对应的文本信息。QRadioButton是一种开关按钮,可以切换为on或off,即checked或unchecked。在单选按钮组里,一次只能选择一个单选按钮,如果需要多个独占的按钮组合,需要将其放到QGroupBox或QButtonGroup中。
import sysfrom PyQt5.QtWidgets import QApplication, QRadioButton, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button1 = QRadioButton("OK", self) button1.toggled.connect(lambda: self.onClicked(button1)) def onClicked(self, button): print("Button {0} is clicked.status is {1}".format(button.text(), button.isChecked()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
3、QCheckBox
QCheckBox继承自QAbstractButton,提供一组带文本标签的复选框,用户可以选择多个选项,复选框可以显示文本和图标。
除了选中、未选中,QCheckBox有第三种状态:半选中,表示没有变化。
import sysfrom PyQt5.QtWidgets import QApplication, QCheckBox, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button1 = QCheckBox("OK", self) button1.stateChanged.connect(lambda: self.onStateChanged(button1)) def onStateChanged(self, button): print("Button {0} is clicked.status is {1}".format(button.text(), button.isChecked()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
六、QComboBox
QComboBox是下拉列表框。
import sysfrom PyQt5.QtWidgets import QApplication, QComboBox, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.combo = QComboBox(self) self.combo.addItem("Apple") self.combo.addItem("HuaWei") self.combo.addItem("XiaoMi") self.combo.addItem("Oppo") self.combo.currentIndexChanged.connect(self.onCurrentIndex) def onCurrentIndex(self, index): print("current item is {0}".format(self.combo.currentText()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
七、QSpinBox
QSpinBox是一个计数器控件,允许用户选择一个整数值,通过单击向上、向下按钮或键盘的上下箭头来增加减少当前显示的值,用户也可以从编辑框输入当前值。默认情况下,QSpinBox的取值范围为0——99,每次改变的步长值为1。
import sysfrom PyQt5.QtWidgets import QApplication, QSpinBox, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) spinBox = QSpinBox(self) spinBox.valueChanged.connect(self.onValueChanged) def onValueChanged(self, value): print("current value is {0}".format(value))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
八、QSlider
QSlider控件提供了一个垂直或水平的滑动条,是一个用于控制有界值的控件,允许用户沿着水平或垂直方向在某一范围内移动滑块,并将滑块所在的位置转换成一个合法范围内的整数值。
import sysfrom PyQt5.QtWidgets import QApplication, QSlider, QWidgetfrom PyQt5.QtCore import Qtclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) slider = QSlider(Qt.Horizontal, self) slider.setMaximum(20) slider.setMinimum(10) slider.valueChanged.connect(self.onValueChanged) def onValueChanged(self, value): print("current value is {0}".format(value))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
九、对话框控件1、QDialog
QDialog是对话框类,提供了三种窗口模态,非模态,模态和应用程序模态,使用setWindowModality方法设置窗口模态。
(1)非模态
非模态可以和应用程序的其它窗×××互,使用Qt.NonModal进行设置。
(2)窗口模态
窗口模态在未处理完成当前对话框时,将阻止和对话框的父窗口进行交互,使用Qt.Modal进行设置。
(3)应用程序模态
应用程序模态阻止任何和其它窗口进行交互,使用Qt.ApplicationModal。
QDialog及其派生类对话框在ESC按键按下时,对话框窗口将会默认调用QDialog.reject方法,关闭对话框。
setWindowModality()方法可以设置窗口是否是模态窗口,Qt::WindowModality默认值为Qt::NonModal,如果没有设置Qt::WindowModality属性值,每次用show()方法显示出的窗口都是非模态窗口。
使用exec()方法显示的对话框为模态对话框,同时会阻塞窗口的响应直到用户关闭对话框,并且返回DialogCode(包括Accepted和Rejected两个值)结果。
如果没有设置Qt::WindowModality属性值,使用exec()方法显示出的对话框默认为应用程序级模态对话框。所有使用exec()方法显示的对话框在窗口关闭前会阻塞整个程序所有窗口的响应。调用exec()方法后,对话框会阻塞,直到对话框关闭才会继续执行。在关闭对话框后exec()方法会返回Accepted或者Rejected,一般程序根据返回不同的结果进行相应的操作。
模式对话框有自己的事件循环,exec() 方法内部先设置modal属性为Qt::ApplicationModal,然后调用 show() 显示对话框,最后启用事件循环来阻止exec() 方法的结束。直到窗口关闭,得到返回结果(DialogCode),退出事件循环,最后exec()方法调用结束,exec()方法后的代码将继续执行。
import sysfrom PyQt5.QtWidgets import QApplication, QDialog, QWidget, QPushButtonfrom PyQt5.QtCore import Qtclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): dialog = QDialog() dialog.setWindowTitle("Dialog Demo") dialog.resize(300, 200) dialog.exec_()if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
2、QMessageBox
QMessageBox是一种通用的弹出式对话框,用于显示消息,允许用户通过点击不同的标准按钮对消息进行反馈,QMessageBox提供了五种常用消息对话框的显示方法。warning(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
创建警告消息对话框critical(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
创建关键错误消息对话框information(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
创建信息消息对话框question(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
创建询问消息对话框about(self, QWidget, p_str, p_str_1)
创建关于信息对话框
import sysfrom PyQt5.QtWidgets import QApplication, QMessageBox, QWidget, QPushButtonclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): button = QMessageBox.question(self, "MessageBox Title", "是否确定关闭?", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) if button == QMessageBox.Ok: print("select Ok Button")if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
3、QInputDialog
QInputDialog是一个标准对话框控件,由一个文本框和两个按钮(OK和Cancel)组成,用户单击OK按钮或按下Enter键后,在父窗口可以接收通过QInputDialog输入的信息。
QInputDialog.getInt从控件中获取标准整型输入
QInputDialog.getItem从控件中获取列表的选项输入
QInputDialog.getText从控件中获取标准字符串输入
QInputDialog.getDouble从控件中获取标准浮点数输入
import sysfrom PyQt5.QtWidgets import QApplication, QInputDialog, QWidget, QPushButtonclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): items = ["C++", "Python", "Java", "Go"] item, ok = QInputDialog.getItem(self, "Select an Item", "Programing Language", items, 0, False) if ok and item: print("selected item: ", item) text, ok = QInputDialog.getText(self, "Input an text", "text:") if ok: print("input text: ", text)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
4、QFontDialog
QFontDialog是字体选择对话框,可以让用户选择所显示的文本的字号大小、样式和格式。QFontDialog.getFont可以从字体选择对话框中获取文本的显示字号、样式和格式。
import sysfrom PyQt5.QtWidgets import QApplication, QFontDialog, QWidget, QPushButtonfrom PyQt5.QtGui import QFontclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): font, ok = QFontDialog.getFont() if ok: print(font.family(), font.pointSize())if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
5、QFileDialog
QFileDialog是用于打开和保存文件的标准对话框,QFileDialog在打开文件时使用文件过滤器,用于显示指定扩展名的文件。
import sysfrom PyQt5.QtWidgets import QApplication, QFileDialog, QWidget, QPushButtonclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): fname, _ = QFileDialog.getOpenFileName(self, "Open file", '/', "Images(*.jpg *.gif)") print(fname)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。