使用信号弹出屏幕
我在使用信号来显示小屏幕时遇到了一些麻烦。 缩短到目前为止我所拥有的一切,下面的代码应该显示我的问题。
import sys
from PyQt4 import QtGui, QtCore
qApp = QtGui.QApplication(sys.argv)
class InformatieVenster(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle('Informatie')
self.setGeometry(100,100,300,200)
informatie = InformatieVenster()
class MenuKlasse(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
about = QtGui.QAction('About...', self)
about.setShortcut('Ctrl+A')
about.setStatusTip('Some text, haha')
self.connect(about, QtCore.SIGNAL('clicked()'), QtCore.SIGNAL(informatie.show()))
menubar = self.menuBar()
self.Menu1 = menubar.addMenu('&File')
self.Menu1.addAction(about)
Menu = MenuKlasse()
Venster = QtGui.QMainWindow()
Venster.menuBar().addMenu(Menu.Menu1)
Venster.setGeometry(200, 200, 300, 300);
size = Venster.geometry()
Venster.show()
qApp.exec_()
运行该程序时,会自动弹出“informatie”窗口。 但是...我只希望每次单击菜单中的“关于...”或使用指定的快捷方式时都会发生这种情况。
我如何改进我的代码,使我的问题成为历史?
问候!
I'm having a little trouble using a signal to make a little screen appear.
Shortening all i have so far, this following code should show my problem.
import sys
from PyQt4 import QtGui, QtCore
qApp = QtGui.QApplication(sys.argv)
class InformatieVenster(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle('Informatie')
self.setGeometry(100,100,300,200)
informatie = InformatieVenster()
class MenuKlasse(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
about = QtGui.QAction('About...', self)
about.setShortcut('Ctrl+A')
about.setStatusTip('Some text, haha')
self.connect(about, QtCore.SIGNAL('clicked()'), QtCore.SIGNAL(informatie.show()))
menubar = self.menuBar()
self.Menu1 = menubar.addMenu('&File')
self.Menu1.addAction(about)
Menu = MenuKlasse()
Venster = QtGui.QMainWindow()
Venster.menuBar().addMenu(Menu.Menu1)
Venster.setGeometry(200, 200, 300, 300);
size = Venster.geometry()
Venster.show()
qApp.exec_()
When this program is runned, the 'informatie' window automatically pops-up.
However... i only want this to happen every time I click on 'about...' in the menu, or when i use the assigned shortcut.
How may i improve my code such that my problem will be made history?
Greets!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显示该窗口是因为您实际上在连接期间调用
.show()
。您必须将函数对象(而不是函数调用的结果)作为参数传递给.connect()
。此外,如果发出信号,要调用的函数称为“槽”,第二个SIGNAL()
完全放错了地方。将连接线替换为:
更好的是,使用现代连接语法:
顺便说一句,不要在 GUI 程序中使用绝对大小。相反,使用布局管理。
The window is shown, because you are actually calling
.show()
during your connect. You have to pass a function object, not the result of a function invocation, as argument to.connect()
. Moreover the function to be invoked, if a signal is emitted, is called "slot", the secondSIGNAL()
is completely misplaced.Replace the connect line with:
Even better, use the modern connection syntax:
Btw, do not use absolute sizes in GUI programs. Instead use layout management.