mpylayer、PyQt4 和 lineEdit
考虑下面的最小示例。它工作得很好,直到我取消注释以下行:
# 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这个简单的示例中,您根本不需要 mainwi。如果
你真的想要它,那么你需要将 mainwi 设置为centralwidget,
不要忘记为 mainwi 添加一些布局,因为这看起来很难看:-)
无论如何,我必须承认,我不知道到底为什么它是否“禁用”按钮。但据我所知,中央小部件必须是窗口的子窗口。
You do not need the mainwi at all in this simple example. Just do
In case you really wanted it, then you need to set the mainwi as the centralwidget
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.