mpylayer、PyQt4 和 lineEdit

发布于 2024-12-20 01:38:51 字数 1644 浏览 6 评论 0原文

考虑下面的最小示例。它工作得很好,直到我取消注释以下行:

# self.mainwi = QtGui.QWidget(self)
# self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
# self.setCentralWidget(self.lineEdit1)

如果这些行被取消注释,我可以在 LineEdit 字段中写入文本,但按钮不会做出反应。知道出了什么问题,如何解决这个问题吗?

我应该补充一点,我绝对是Python编程的初学者。

#!/usr/bin/python

import mpylayer
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui



class DmplayerGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.dirty = False
        self.mp = mpylayer.MPlayerControl()
        #Toolbar
        ## items

        ### Play
        self.play = QtGui.QAction(QtGui.QIcon('icons/play_32.png'), 'Play', self)
        self.play.setShortcut('Ctrl+A')
        self.connect(self.play, QtCore.SIGNAL('triggered()'), self.DPlay)

        ### Pause
        self.pause = QtGui.QAction(QtGui.QIcon('icons/pause_32.png'), 'Pause', self)
        self.pause.setShortcut('Ctrl+P')
        self.connect(self.pause, QtCore.SIGNAL('triggered()'), self.DPause)


        ## toolbar
        self.toolbar = self.addToolBar('Toolbar')
        self.toolbar.addAction(self.play)
        self.toolbar.addAction(self.pause)

        # self.mainwi = QtGui.QWidget(self)
        # self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
        # self.setCentralWidget(self.lineEdit1)

    # play
    def DPlay(self):
        self.mp.loadfile('video.mp4')

    # pause
    def DPause(self):
        self.mp.pause(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    dp = DmplayerGUI()
    dp.show()
    sys.exit(app.exec_())

Consider the minimal example below. It works perfectly until I uncomment the following lines:

# self.mainwi = QtGui.QWidget(self)
# self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
# self.setCentralWidget(self.lineEdit1)

If those lines are uncommented, I can write text in the LineEdit-field, but the buttons don't react. Any idea what's wrong with it, how to fix this?

I should add that I am an absolute beginner in programming python.

#!/usr/bin/python

import mpylayer
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui



class DmplayerGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.dirty = False
        self.mp = mpylayer.MPlayerControl()
        #Toolbar
        ## items

        ### Play
        self.play = QtGui.QAction(QtGui.QIcon('icons/play_32.png'), 'Play', self)
        self.play.setShortcut('Ctrl+A')
        self.connect(self.play, QtCore.SIGNAL('triggered()'), self.DPlay)

        ### Pause
        self.pause = QtGui.QAction(QtGui.QIcon('icons/pause_32.png'), 'Pause', self)
        self.pause.setShortcut('Ctrl+P')
        self.connect(self.pause, QtCore.SIGNAL('triggered()'), self.DPause)


        ## toolbar
        self.toolbar = self.addToolBar('Toolbar')
        self.toolbar.addAction(self.play)
        self.toolbar.addAction(self.pause)

        # self.mainwi = QtGui.QWidget(self)
        # self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
        # self.setCentralWidget(self.lineEdit1)

    # play
    def DPlay(self):
        self.mp.loadfile('video.mp4')

    # pause
    def DPause(self):
        self.mp.pause(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    dp = DmplayerGUI()
    dp.show()
    sys.exit(app.exec_())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏夜暖风 2024-12-27 01:38:51

在这个简单的示例中,您根本不需要 mainwi。如果

self.lineEdit1 = QtGui.QLineEdit(self)
self.setCentralWidget(self.lineEdit1)

你真的想要它,那么你需要将 mainwi 设置为centralwidget,

self.mainwi = QtGui.QWidget(self)
self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
self.setCentralWidget(self.mainwi)

不要忘记为 mainwi 添加一些布局,因为这看起来很难看:-)

无论如何,我必须承认,我不知道到底为什么它是否“禁用”按钮。但据我所知,中央小部件必须是窗口的子窗口。

You do not need the mainwi at all in this simple example. Just do

self.lineEdit1 = QtGui.QLineEdit(self)
self.setCentralWidget(self.lineEdit1)

In case you really wanted it, then you need to set the mainwi as the centralwidget

self.mainwi = QtGui.QWidget(self)
self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
self.setCentralWidget(self.mainwi)

do not forget to add some layout for mainwi, since this looks ugly :-)

Anyway, I have to admit, that I do not know why exactly does it "disable" the buttons. But the central widget has to be a child of the window as far as I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文