我有这个文件让我们说widgetA.py
import sys, requests, json
from PyQt5.QtWidgets import (
QApplication, QWidget, QDesktopWidget
)
class main(QWidget):
def __init__(self, parent=None):
super(main, self).__init__(parent)
self.initUI()
def initUI(self):
self.setFixedSize(254, 380)
self.center()
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == "__main__":
app = QApplication(sys.argv)
x = main()
sys.exit(app.exec_())
如何从另一个小部件打开该小部件,让我们使用QPushButton来表示widgetB.py
widgetB.py具有完全相同的结构,并添加了一个按钮
import widgetA
...
class main(QWidget):
....
def initUI(self):
openwidget = QPushbutton('open', self)
openwidget.clicked.connect(widgetA.show)
self.setFixedSize(254, 380)
self.center()
self.show()
....
....
答案 0 :(得分:0)
nvm我是一个这样的白痴似乎你不能两次宣布QWidget所以我把其中一个改为QDialog,我还在学习所以请耐心等待我X__X
widgeta.py:
class widgetA(QDialog):
def __init__(self, parent=None):
super(widgetA, self).__init__(parent)
self.initUI()
然后这是在widgetb.py:
from widgeta import widgetA
class widgetB(QWidget):
def __init__(self, parent=None):
super(widgetB, self).__init__(parent)
self.initUI()
self.show()
def initUI(self):
self.setFixedSize(254, 380)
self.center()
button = QPushButton('open', self)
button.clicked.connect(self.openwindow)
self.openwindow = widgetA()
def openwindow(self):
self.openwindow.exec()