[英]PyQt5 Custom Widget Opens in Another Window
I started out with PyQt5 recently.我最近开始使用 PyQt5。 I wanted to create a custom widget and then insert it into the main window of an application.
我想创建一个自定义小部件,然后将其插入到应用程序的主 window 中。
The custom Widget:自定义小部件:
class ScoreCard(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ScoreCard, self).__init__(parent=parent)
self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
self.pressing = False
self.init_ui()
self.show()
def init_ui(self):
# Layout in here
And this is the main Application:这是主要的应用程序:
from PyQt5.QtWidgets import *
from scorecard import ScoreCard
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUi()
self.show()
def initUi(self):
self.setGeometry(300,300,800,700)
window_layout = QVBoxLayout()
recent_playcard = ScoreCard()
window_layout.addWidget(recent_playcard)
self.setLayout(window_layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
execute = MainWindow()
sys.exit(app.exec_())
Why is it that whenever I run the main Application, the custom widget appears in another window?为什么每当我运行主应用程序时,自定义小部件都会出现在另一个 window 中? I even tried removing the frame and setting the parent to none, but none of that changed this behavior.
我什至尝试删除框架并将父级设置为无,但这些都没有改变这种行为。 How do I fix this?
我该如何解决?
Looks like I mixed up QMainWindow
and QDialog
I should be using a central widget for the main application instead of setting a layout..看起来我混合了
QMainWindow
和QDialog
我应该为主应用程序使用中央小部件而不是设置布局..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.