Python 使用PyQt和Qt设计器ui文件
我是PyQt新手,我正在尝试直接从PyQt脚本中使用ui文件。我有两个ui文件,mainwindow.ui和landing.ui。点击主窗口上的“按钮”应打开着陆窗口。但是,单击按钮并不像我预期的那样工作。下面是代码(我只是想解决一些问题,所以代码很粗糙):Python 使用PyQt和Qt设计器ui文件,python,pyqt,pyqt4,qt-designer,Python,Pyqt,Pyqt4,Qt Designer,我是PyQt新手,我正在尝试直接从PyQt脚本中使用ui文件。我有两个ui文件,mainwindow.ui和landing.ui。点击主窗口上的“按钮”应打开着陆窗口。但是,单击按钮并不像我预期的那样工作。下面是代码(我只是想解决一些问题,所以代码很粗糙): 我犯了什么错误???您的脚本有两个主要问题:第一,您没有正确构建ui文件的路径;其次,您没有保留对登录页窗口的引用(因此在显示该窗口后,它将立即被垃圾回收) 下面是加载ui文件的脚本部分的结构: import os from PyQt4 i
我犯了什么错误???您的脚本有两个主要问题:第一,您没有正确构建ui文件的路径;其次,您没有保留对登录页窗口的引用(因此在显示该窗口后,它将立即被垃圾回收) 下面是加载ui文件的脚本部分的结构:
import os
from PyQt4 import QtCore, QtGui, uic
# get the directory of this script
path = os.path.dirname(os.path.abspath(__file__))
MainWindowUI, MainWindowBase = uic.loadUiType(
os.path.join(path, 'mainwindow.ui'))
LandingPageUI, LandingPageBase = uic.loadUiType(
os.path.join(path, 'landing.ui'))
class MainWindow(MainWindowBase, MainWindowUI):
def __init__(self, parent=None):
MainWindowBase.__init__(self, parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.handleButton)
def handleButton(self):
# keep a reference to the landing page
self.landing = LandingPage()
self.landing.show()
class LandingPage(LandingPageBase, LandingPageUI):
def __init__(self, parent=None):
LandingPageBase.__init__(self, parent)
self.setupUi(self)
@Wedava如果此问题已解决,请不要忘记接受答案(如果可以,请进行投票)@ekhumoro我可以编辑此解决方案以使
登录页
显示为停靠小部件吗?@Kajsa。当然-只需在主窗口中创建一个QDockWidget
和一个LandingPage
。\uuuuu init\uuuuuu
,然后执行self.dockWidget.setWidget(self.LandingPage)
@ekhumaro有没有使用QWidget
的方法?我不希望小部件是可拆卸的。@Kajsa。使用dockWidget.setFeatures(QDockWidget.NoDockWidgetFeatures)
-这意味着它不能被移动、浮动或关闭。
import os
from PyQt4 import QtCore, QtGui, uic
# get the directory of this script
path = os.path.dirname(os.path.abspath(__file__))
MainWindowUI, MainWindowBase = uic.loadUiType(
os.path.join(path, 'mainwindow.ui'))
LandingPageUI, LandingPageBase = uic.loadUiType(
os.path.join(path, 'landing.ui'))
class MainWindow(MainWindowBase, MainWindowUI):
def __init__(self, parent=None):
MainWindowBase.__init__(self, parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.handleButton)
def handleButton(self):
# keep a reference to the landing page
self.landing = LandingPage()
self.landing.show()
class LandingPage(LandingPageBase, LandingPageUI):
def __init__(self, parent=None):
LandingPageBase.__init__(self, parent)
self.setupUi(self)