【概览】
1、显示原生Qwidget
1)不使用布局(绝对定位)
2)使用布局
2、显示Qwidget的自定义类
1)不使用布局(绝对定位)
2)使用布局
【知识点】
1、显示原生Qwidget
1)不使用布局(绝对定位)
这种情况下,原生QWidget部件在实例化时必须带parent参数,当然parent = self,即: self.widget = QWidget(self)
1 class MyWindow(QWidget): 2 def __init__(self, parent=None): 3 super(MyWindow,self).__init__(parent) 4 self.resize(400, 300) 5 6 # 添加原生QWidget 7 self.widget = QWidget(self) # 注意QWidget(self) 内的self!! 8 self.widget.setGeometry(10,10,380,250) 9 self.widget.setStyleSheet("background-color:grey;") 10 11 # 添加编辑框(QLineEdit) 12 self.lineEdit = QLineEdit("0",self) # 注意QLineEdit("0",self) 内的self!! 13 self.lineEdit.setGeometry(10,270,380,20)
【效果图】
2)使用布局
这种情况下,原生QWidget部件在实例化时可以不带parent参数,parent =None / self都行,即: self.widget = QWidget()或 self.widget = QWidget(self)
class MyWindow(QWidget): def __init__(self, parent=None): super(MyWindow,self).__init__(parent) self.resize(400, 300) layout = QGridLayout() # 添加原生QWidget self.widget = QWidget() # 注意QWidget() 内可以没有self!! self.widget.setStyleSheet("background-color:grey;") # 添加编辑框(QLineEdit) self.lineEdit = QLineEdit("0") # 注意QLineEdit("0") 内可以没self!! # 放入布局内 layout.addWidget(self.widget,0,0) layout.addWidget(self.lineEdit,1,0) self.setLayout(layout)
【效果图】
2、显示Qwidget的自定义类
1)不使用布局(绝对定位)
这种情况下,原生QWidget自定义类里面要放个东西!!不放东西的话,我也不会:(
1 class MyWidget(QWidget): 2 def __init__(self, parent=None): 3 super(MyWidget,self).__init__(parent) 4 5 #QWidget自定义类里面要放个东西!!(不放东西的话,我也不会) 6 self.gb = QGroupBox(self) 7 self.gb.setGeometry(0,0,200,200) #一定要有个东西把它撑起来!!不然看不到 8 self.setStyleSheet("background-color:grey;") 9 10 self.do_something() 11 12 def do_something(self): 13 pass 14 15 16 class MyWindow(QWidget): 17 def __init__(self, parent=None): 18 super(MyWindow,self).__init__(parent) 19 self.resize(400,300) 20 21 # 添加自定义部件(MyWidget) 22 self.widget = MyWidget(self) 23 self.widget.setGeometry(10,10,380,240) 24 25 26 # 添加编辑框(QLineEdit) 27 self.lineEdit = QLineEdit("0",self) 28 self.lineEdit.setGeometry(10,260,380,20) 29 30
【效果图】
2)使用布局
这种情况下,原生QWidget部件在实例化时可以不带parent参数,parent =None / self都行,即: self.widget = QWidget()或 self.widget = QWidget(self)
1 class MyWindow(QWidget): 2 def __init__(self, parent=None): 3 super(MyWindow,self).__init__(parent) 4 self.resize(400,300) 5 layout = QGridLayout() 6 7 # 添加自定义部件(MyWidget) 8 self.widget = MyWidget() 9 10 # 添加编辑框(QLineEdit) 11 self.lineEdit = QLineEdit("0") 12 13 # 放入布局内 14 layout.addWidget(self.widget,0,0) 15 layout.addWidget(self.lineEdit,1,0) 16 self.setLayout(layout) 17 18 self.setWindowTitle("4、QWidget的自定义类")
【效果图】
【源代码】(依次)
1 from PyQt5.QtCore import * 2 from PyQt5.QtGui import * 3 from PyQt5.QtWidgets import * 4 5 6 7 class MyWindow(QWidget): 8 def __init__(self): 9 super(MyWindow,self).__init__() 10 self.resize(400, 300) 11 12 # 添加原生QWidget 13 self.widget = QWidget(self) # 注意QWidget(self) 内的self!! 14 self.widget.setGeometry(10,10,380,250) 15 self.widget.setStyleSheet("background-color:grey;") 16 17 # 添加编辑框(QLineEdit) 18 self.lineEdit = QLineEdit("0",self) # 注意QLineEdit("0",self) 内的self!! 19 self.lineEdit.setGeometry(10,270,380,20) 20 21 self.setWindowTitle("1、原生QWidget") 22 23 24 if __name__ == ‘__main__‘: 25 import sys 26 app = QApplication(sys.argv) 27 dialog = MyWindow() 28 dialog.show(); 29 sys.exit(app.exec_())
1 from PyQt5.QtCore import * 2 from PyQt5.QtGui import * 3 from PyQt5.QtWidgets import * 4 5 6 7 class MyWindow(QWidget): 8 def __init__(self): 9 super(MyWindow,self).__init__() 10 self.resize(400, 300) 11 layout = QGridLayout() 12 13 # 添加原生QWidget 14 self.widget = QWidget() 15 self.widget.setStyleSheet("background-color:grey;") 16 17 # 添加编辑框(QLineEdit) 18 self.lineEdit = QLineEdit("0") 19 20 # 放入布局内 21 layout.addWidget(self.widget,0,0) 22 layout.addWidget(self.lineEdit,1,0) 23 self.setLayout(layout) 24 25 self.setWindowTitle("2、原生QWidget") 26 27 if __name__ == ‘__main__‘: 28 import sys 29 app = QApplication(sys.argv) 30 dialog = MyWindow() 31 dialog.show(); 32 sys.exit(app.exec_())
1 from PyQt5.QtCore import * 2 from PyQt5.QtGui import * 3 from PyQt5.QtWidgets import * 4 5 6 class MyWidget(QWidget): 7 def __init__(self, parent=None): 8 super(MyWidget,self).__init__(parent) 9 10 #QWidget派生类里面要放个东西!!(不放东西的话,我也不会) 11 self.gb = QGroupBox(self) 12 self.gb.setGeometry(0,0,200,200) #一定要有个东西把它撑起来!!不然看不到 13 self.setStyleSheet("background-color:grey;") 14 15 self.do_something() 16 17 def do_something(self): 18 pass 19 20 21 class MyWindow(QWidget): 22 def __init__(self, parent=None): 23 super(MyWindow,self).__init__(parent) 24 self.resize(400,300) 25 26 # 添加自定义部件(MyWidget) 27 self.widget = MyWidget(self) 28 self.widget.setGeometry(10,10,380,240) 29 30 31 # 添加编辑框(QLineEdit) 32 self.lineEdit = QLineEdit("0",self) 33 self.lineEdit.setGeometry(10,260,380,20) 34 35 self.setWindowTitle("3、QWidget的派生类") 36 37 if __name__ == ‘__main__‘: 38 import sys 39 app = QApplication(sys.argv) 40 window = MyWindow() 41 window.show(); 42 sys.exit(app.exec_())
1 from PyQt5.QtCore import * 2 from PyQt5.QtGui import * 3 from PyQt5.QtWidgets import * 4 5 class MyWidget(QWidget): 6 def __init__(self, parent=None): 7 super(MyWidget,self).__init__(parent) 8 9 #QWidget派生类里面要放个东西!!(不放东西的话,我也不会) 10 self.gb = QGroupBox(self) 11 self.gb.setGeometry(0,0,200,200) #一定要有个东西把它撑起来!!不然看不到 12 self.setStyleSheet("background-color:red;") 13 self.do_something() 14 15 def do_something(self): 16 pass 17 18 19 20 class MyWindow(QWidget): 21 def __init__(self): 22 super(MyWindow,self).__init__() 23 self.resize(400,300) 24 layout = QGridLayout() 25 26 # 添加自定义部件(MyWidget) 27 self.widget = MyWidget(self) # sizeHint() = QSize(-1, -1) 28 self.widget.setStyleSheet("background-color:red;") 29 30 # 添加编辑框(QLineEdit) 31 self.lineEdit = QLineEdit("0",self) # sizeHint() = QSize(75, 20) 32 33 # 放入布局内 34 layout.addWidget(self.widget,0,0) 35 layout.addWidget(self.lineEdit,1,0) 36 self.setLayout(layout) 37 38 39 40 if __name__ == ‘__main__‘: 41 import sys 42 app = QApplication(sys.argv) 43 window = MyWindow() 44 window.show(); 45 sys.exit(app.exec_())
时间: 2024-10-03 14:55:51