pyqt5学习笔记(4) QWidget
程序员文章站
2022-05-28 11:02:28
...
QWidget
基础窗口控件QWiget类是所有用户界面对象的基类,所有的窗口和空间都是直接或者间接继承自QWiget类。QWidget继承自 QObject 和 PaintDevice 。
1 常用方法
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: Lancer 2020-05-08 08:45:33
from PyQt5.QtWidgets import QMessageBox,QPushButton,QVBoxLayout,QWidget,QApplication,QPlainTextEdit,QHBoxLayout
from PyQt5.QtCore import *
class QWidgetDemo(QWidget):
def __init__(self):
super(QWidgetDemo, self).__init__()
self.resize(800,600) #不含边框
self.setWindowTitle("QWidget学习笔记")
mainlayout = QVBoxLayout()
self.setLayout(mainlayout)
####
self.show_textedit = QPlainTextEdit()
self.show_textedit.setReadOnly(True)
h_layout = QHBoxLayout()
mainlayout.addWidget(self.show_textedit)
self.show_str = ""
#QWidget不含边框的常用函数
self.show_str += str(self.size())
self.show_str += " with: %d height:%d "%(self.width(),self.height())
## 含边框函数
self.show_str +="\n"+ str(self.frameGeometry()) #获得窗口的大小和位置
self.show_str += "\n" + "不含边框 宽度:%d 高度:%d "%(self.geometry().width(),self.geometry().height() ) #不行边框
self.show_str += "\n" + "x,y获取客户区的左上角坐标(包含窗口的标题与边框部分) x:%d y:%d " % (self.geometry().x(), self.geometry().y())
self.show_str += "\n" + "maximumHeight: %d maximumWidth:%d "%(self.maximumHeight(), self.maximumWidth())
self.show_str += "\n" + "minimumHeight: %d minimumWidth:%d " % (self.minimumHeight(), self.minimumWidth())
self.show_str += "\n" + str(self.layout()) + '\nlayout: ' + str(self.layoutDirection())
self.show_textedit.setPlainText(self.show_str)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
test = QWidgetDemo()
test.show()
sys.exit(app.exec_())