将PyQt按钮与Python动作链接

时间:2015-03-10 02:13:55

标签: python qt user-interface pyqt

我无法在QT Designer中设置GUI并将其转换为python脚本,以便在点击时执行任何操作。

目前我的代码看起来像这样:

import sys
from PyQt4.QtGui import QApplication, QDialog
from punchmenuui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSignal

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.closemenubutton.clicked.connect(self.closemenu)

def closemenu(self):
    sys.exit()

app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())

如你所见,它很简单。我在布局.py文件中指定了一个按钮,我试图抓住"点击"从它发出信号并触发一个干净的退出事件。问题是:似乎没有任何成就。 我尝试过新旧风格的PyQt信号/插槽绑定都无济于事。

有人能发现我做错了吗?

编辑:

感谢你们迄今为止的回复。我已经尝试了所有答案的所有组合,但到目前为止它们都没有奏效。我当前的启动器.py脚本代码如下:

import sys
from PyQt4.QtGui import QApplication, QDialog
from punchmenuui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSignal

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.closemenubutton.clicked.connect(self.closemenu)

    def closemenu():
        sys.exit()

app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())

和QT-Designed ui的脚本是:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'punchmenu.ui'
#
# Created: Tue Mar 10 01:30:31 2015
#      by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(480, 320)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tabWidget = QtGui.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(-1, 0, 481, 301))
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.tabWidget.sizePolicy().hasHeightForWidth())
        self.tabWidget.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.tabWidget.setFont(font)
        self.tabWidget.setStyleSheet(_fromUtf8("QTabBar::scroller { /* the width of the scroll buttons */ width: 50px; } "))
        self.tabWidget.setTabPosition(QtGui.QTabWidget.North)
        self.tabWidget.setTabShape(QtGui.QTabWidget.Rounded)
        self.tabWidget.setElideMode(QtCore.Qt.ElideNone)
        self.tabWidget.setUsesScrollButtons(True)
        self.tabWidget.setDocumentMode(True)
        self.tabWidget.setTabsClosable(False)
        self.tabWidget.setMovable(False)
        self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
        self.hometab = QtGui.QWidget()
        self.hometab.setObjectName(_fromUtf8("hometab"))
        self.label = QtGui.QLabel(self.hometab)
        self.label.setGeometry(QtCore.QRect(60, 0, 371, 231))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("vtc punch logo.jpg")))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.hometab)
        self.label_2.setGeometry(QtCore.QRect(10, 240, 81, 31))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.flipscreenbutton = QtGui.QPushButton(self.hometab)
        self.flipscreenbutton.setGeometry(QtCore.QRect(150, 240, 191, 31))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.flipscreenbutton.setFont(font)
        self.flipscreenbutton.setObjectName(_fromUtf8("flipscreenbutton"))
        self.tabWidget.addTab(self.hometab, _fromUtf8(""))
        self.powertab = QtGui.QWidget()
        self.powertab.setObjectName(_fromUtf8("powertab"))
        self.groupBox_5 = QtGui.QGroupBox(self.powertab)
        self.groupBox_5.setGeometry(QtCore.QRect(10, 10, 461, 261))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_5.setFont(font)
        self.groupBox_5.setObjectName(_fromUtf8("groupBox_5"))
        self.restartbutton = QtGui.QPushButton(self.groupBox_5)
        self.restartbutton.setGeometry(QtCore.QRect(130, 20, 181, 111))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.restartbutton.setFont(font)
        self.restartbutton.setObjectName(_fromUtf8("restartbutton"))
        self.shutdownbutton = QtGui.QPushButton(self.groupBox_5)
        self.shutdownbutton.setGeometry(QtCore.QRect(130, 140, 181, 111))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.shutdownbutton.setFont(font)
        self.shutdownbutton.setObjectName(_fromUtf8("shutdownbutton"))
        self.tabWidget.addTab(self.powertab, _fromUtf8(""))
        self.lanipconfigtab = QtGui.QWidget()
        self.lanipconfigtab.setObjectName(_fromUtf8("lanipconfigtab"))
        self.groupBox = QtGui.QGroupBox(self.lanipconfigtab)
        self.groupBox.setGeometry(QtCore.QRect(10, 10, 461, 261))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox.setFont(font)
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.dhcpbutton = QtGui.QPushButton(self.groupBox)
        self.dhcpbutton.setGeometry(QtCore.QRect(10, 80, 201, 91))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.dhcpbutton.setFont(font)
        self.dhcpbutton.setObjectName(_fromUtf8("dhcpbutton"))
        self.staticbutton = QtGui.QPushButton(self.groupBox)
        self.staticbutton.setGeometry(QtCore.QRect(250, 80, 201, 91))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.staticbutton.setFont(font)
        self.staticbutton.setObjectName(_fromUtf8("staticbutton"))
        self.tabWidget.addTab(self.lanipconfigtab, _fromUtf8(""))
        self.wificonfigtab = QtGui.QWidget()
        self.wificonfigtab.setObjectName(_fromUtf8("wificonfigtab"))
        self.scrollArea = QtGui.QScrollArea(self.wificonfigtab)
        self.scrollArea.setGeometry(QtCore.QRect(0, 0, 481, 281))
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 477, 277))
        self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
        self.groupBox_9 = QtGui.QGroupBox(self.scrollAreaWidgetContents)
        self.groupBox_9.setGeometry(QtCore.QRect(0, 10, 471, 261))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_9.setFont(font)
        self.groupBox_9.setObjectName(_fromUtf8("groupBox_9"))
        self.layoutWidget = QtGui.QWidget(self.groupBox_9)
        self.layoutWidget.setGeometry(QtCore.QRect(10, 20, 451, 241))
        self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.scanforaccesspointsbutton = QtGui.QPushButton(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(16)
        self.scanforaccesspointsbutton.setFont(font)
        self.scanforaccesspointsbutton.setObjectName(_fromUtf8("scanforaccesspointsbutton"))
        self.verticalLayout.addWidget(self.scanforaccesspointsbutton)
        self.connectwifibutton = QtGui.QPushButton(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(16)
        self.connectwifibutton.setFont(font)
        self.connectwifibutton.setObjectName(_fromUtf8("connectwifibutton"))
        self.verticalLayout.addWidget(self.connectwifibutton)
        self.disconnectwifibutton = QtGui.QPushButton(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(16)
        self.disconnectwifibutton.setFont(font)
        self.disconnectwifibutton.setObjectName(_fromUtf8("disconnectwifibutton"))
        self.verticalLayout.addWidget(self.disconnectwifibutton)
        self.erasestoredwifinetworksbutton = QtGui.QPushButton(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(16)
        self.erasestoredwifinetworksbutton.setFont(font)
        self.erasestoredwifinetworksbutton.setObjectName(_fromUtf8("erasestoredwifinetworksbutton"))
        self.verticalLayout.addWidget(self.erasestoredwifinetworksbutton)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.tabWidget.addTab(self.wificonfigtab, _fromUtf8(""))
        self.captivportallogintab = QtGui.QWidget()
        self.captivportallogintab.setObjectName(_fromUtf8("captivportallogintab"))
        self.webView = QtWebKit.QWebView(self.captivportallogintab)
        self.webView.setGeometry(QtCore.QRect(0, 0, 481, 271))
        self.webView.setUrl(QtCore.QUrl(_fromUtf8("http://www.briancourt.co.uk/")))
        self.webView.setZoomFactor(0.40000000596)
        self.webView.setRenderHints(QtGui.QPainter.SmoothPixmapTransform|QtGui.QPainter.TextAntialiasing)
        self.webView.setObjectName(_fromUtf8("webView"))
        self.tabWidget.addTab(self.captivportallogintab, _fromUtf8(""))
        self.wifiapconfigtab = QtGui.QWidget()
        self.wifiapconfigtab.setObjectName(_fromUtf8("wifiapconfigtab"))
        self.groupBox_4 = QtGui.QGroupBox(self.wifiapconfigtab)
        self.groupBox_4.setGeometry(QtCore.QRect(10, 10, 461, 261))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_4.setFont(font)
        self.groupBox_4.setObjectName(_fromUtf8("groupBox_4"))
        self.tabWidget.addTab(self.wifiapconfigtab, _fromUtf8(""))
        self.vpnconfigtab = QtGui.QWidget()
        self.vpnconfigtab.setObjectName(_fromUtf8("vpnconfigtab"))
        self.groupBox_6 = QtGui.QGroupBox(self.vpnconfigtab)
        self.groupBox_6.setGeometry(QtCore.QRect(10, 10, 461, 261))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_6.setFont(font)
        self.groupBox_6.setObjectName(_fromUtf8("groupBox_6"))
        self.disconnectvpnbutton = QtGui.QPushButton(self.groupBox_6)
        self.disconnectvpnbutton.setGeometry(QtCore.QRect(10, 150, 211, 91))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.disconnectvpnbutton.setFont(font)
        self.disconnectvpnbutton.setObjectName(_fromUtf8("disconnectvpnbutton"))
        self.connectvpnbutton = QtGui.QPushButton(self.groupBox_6)
        self.connectvpnbutton.setGeometry(QtCore.QRect(240, 150, 211, 91))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.connectvpnbutton.setFont(font)
        self.connectvpnbutton.setObjectName(_fromUtf8("connectvpnbutton"))
        self.switchtotcpbutton = QtGui.QPushButton(self.groupBox_6)
        self.switchtotcpbutton.setGeometry(QtCore.QRect(240, 40, 211, 91))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.switchtotcpbutton.setFont(font)
        self.switchtotcpbutton.setObjectName(_fromUtf8("switchtotcpbutton"))
        self.switchtoudpbutton = QtGui.QPushButton(self.groupBox_6)
        self.switchtoudpbutton.setGeometry(QtCore.QRect(10, 40, 211, 91))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.switchtoudpbutton.setFont(font)
        self.switchtoudpbutton.setObjectName(_fromUtf8("switchtoudpbutton"))
        self.tabWidget.addTab(self.vpnconfigtab, _fromUtf8(""))
        self.statusdisplaytab = QtGui.QWidget()
        self.statusdisplaytab.setObjectName(_fromUtf8("statusdisplaytab"))
        self.lanipbox = QtGui.QGroupBox(self.statusdisplaytab)
        self.lanipbox.setGeometry(QtCore.QRect(10, 0, 461, 71))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.lanipbox.setFont(font)
        self.lanipbox.setObjectName(_fromUtf8("lanipbox"))
        self.lanipshow = QtGui.QLabel(self.lanipbox)
        self.lanipshow.setGeometry(QtCore.QRect(10, 20, 451, 41))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.lanipshow.setFont(font)
        self.lanipshow.setAlignment(QtCore.Qt.AlignCenter)
        self.lanipshow.setObjectName(_fromUtf8("lanipshow"))
        self.groupBox_2 = QtGui.QGroupBox(self.statusdisplaytab)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 80, 461, 91))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_2.setFont(font)
        self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))
        self.wifiipshow = QtGui.QLabel(self.groupBox_2)
        self.wifiipshow.setGeometry(QtCore.QRect(10, 30, 451, 41))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.wifiipshow.setFont(font)
        self.wifiipshow.setAlignment(QtCore.Qt.AlignCenter)
        self.wifiipshow.setObjectName(_fromUtf8("wifiipshow"))
        self.groupBox_3 = QtGui.QGroupBox(self.statusdisplaytab)
        self.groupBox_3.setGeometry(QtCore.QRect(10, 180, 461, 91))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_3.setFont(font)
        self.groupBox_3.setObjectName(_fromUtf8("groupBox_3"))
        self.vpnipshow = QtGui.QLabel(self.groupBox_3)
        self.vpnipshow.setGeometry(QtCore.QRect(10, 30, 451, 41))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.vpnipshow.setFont(font)
        self.vpnipshow.setAlignment(QtCore.Qt.AlignCenter)
        self.vpnipshow.setObjectName(_fromUtf8("vpnipshow"))
        self.tabWidget.addTab(self.statusdisplaytab, _fromUtf8(""))
        self.softwareupdatetab = QtGui.QWidget()
        self.softwareupdatetab.setObjectName(_fromUtf8("softwareupdatetab"))
        self.label_7 = QtGui.QLabel(self.softwareupdatetab)
        self.label_7.setGeometry(QtCore.QRect(50, 40, 311, 41))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_7.setFont(font)
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.label_8 = QtGui.QLabel(self.softwareupdatetab)
        self.label_8.setGeometry(QtCore.QRect(370, 40, 121, 41))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label_8.setFont(font)
        self.label_8.setObjectName(_fromUtf8("label_8"))
        self.groupBox_7 = QtGui.QGroupBox(self.softwareupdatetab)
        self.groupBox_7.setGeometry(QtCore.QRect(10, 9, 461, 261))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_7.setFont(font)
        self.groupBox_7.setObjectName(_fromUtf8("groupBox_7"))
        self.checkforupdatebutton = QtGui.QPushButton(self.groupBox_7)
        self.checkforupdatebutton.setGeometry(QtCore.QRect(110, 90, 231, 91))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.checkforupdatebutton.setFont(font)
        self.checkforupdatebutton.setObjectName(_fromUtf8("checkforupdatebutton"))
        self.tabWidget.addTab(self.softwareupdatetab, _fromUtf8(""))
        self.devmenutab = QtGui.QWidget()
        self.devmenutab.setObjectName(_fromUtf8("devmenutab"))
        self.groupBox_8 = QtGui.QGroupBox(self.devmenutab)
        self.groupBox_8.setGeometry(QtCore.QRect(9, 10, 461, 261))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_8.setFont(font)
        self.groupBox_8.setObjectName(_fromUtf8("groupBox_8"))
        self.closemenubutton = QtGui.QPushButton(self.groupBox_8)
        self.closemenubutton.setGeometry(QtCore.QRect(20, 40, 191, 61))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.closemenubutton.setFont(font)
        self.closemenubutton.setObjectName(_fromUtf8("closemenubutton"))
        self.iteratenewpunch = QtGui.QPushButton(self.groupBox_8)
        self.iteratenewpunch.setGeometry(QtCore.QRect(20, 180, 421, 61))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.iteratenewpunch.setFont(font)
        self.iteratenewpunch.setObjectName(_fromUtf8("iteratenewpunch"))
        self.cloneexistingpunchbutton = QtGui.QPushButton(self.groupBox_8)
        self.cloneexistingpunchbutton.setGeometry(QtCore.QRect(20, 110, 421, 61))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.cloneexistingpunchbutton.setFont(font)
        self.cloneexistingpunchbutton.setObjectName(_fromUtf8("cloneexistingpunchbutton"))
        self.restartmenubutton = QtGui.QPushButton(self.groupBox_8)
        self.restartmenubutton.setGeometry(QtCore.QRect(250, 40, 191, 61))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.restartmenubutton.setFont(font)
        self.restartmenubutton.setObjectName(_fromUtf8("restartmenubutton"))
        self.tabWidget.addTab(self.devmenutab, _fromUtf8(""))
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setSizeGripEnabled(False)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(9)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Punch Menu", None))
        self.label_2.setText(_translate("MainWindow", "Beta 2", None))
        self.flipscreenbutton.setText(_translate("MainWindow", "Flip Screen", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.hometab), _translate("MainWindow", "Home", None))
        self.groupBox_5.setTitle(_translate("MainWindow", "Select Action", None))
        self.restartbutton.setText(_translate("MainWindow", "Restart", None))
        self.shutdownbutton.setText(_translate("MainWindow", "Shutdown", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.powertab), _translate("MainWindow", "Power", None))
        self.groupBox.setTitle(_translate("MainWindow", "Select Connection Type", None))
        self.dhcpbutton.setText(_translate("MainWindow", "DHCP (Auto)", None))
        self.staticbutton.setText(_translate("MainWindow", "Static (Manual)", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.lanipconfigtab), _translate("MainWindow", "LAN IP Config", None))
        self.groupBox_9.setTitle(_translate("MainWindow", "Select Action", None))
        self.scanforaccesspointsbutton.setText(_translate("MainWindow", "Scan for Access Points", None))
        self.connectwifibutton.setText(_translate("MainWindow", "Connect Wifi", None))
        self.disconnectwifibutton.setText(_translate("MainWindow", "Disconnect Wifi", None))
        self.erasestoredwifinetworksbutton.setText(_translate("MainWindow", "Erase Stored Wifi Networks", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.wificonfigtab), _translate("MainWindow", "Wifi IP Config", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.captivportallogintab), _translate("MainWindow", "Captive Portal Login", None))
        self.groupBox_4.setTitle(_translate("MainWindow", "Feature Not Yet Implemented", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.wifiapconfigtab), _translate("MainWindow", "Wifi A/P Config", None))
        self.groupBox_6.setTitle(_translate("MainWindow", "Select Action", None))
        self.disconnectvpnbutton.setText(_translate("MainWindow", "Disconnect VPN", None))
        self.connectvpnbutton.setText(_translate("MainWindow", "Connect VPN", None))
        self.switchtotcpbutton.setText(_translate("MainWindow", "Switch to TCP", None))
        self.switchtoudpbutton.setText(_translate("MainWindow", "Switch to UDP", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.vpnconfigtab), _translate("MainWindow", "VPN Config", None))
        self.lanipbox.setTitle(_translate("MainWindow", "LAN IP", None))
        self.lanipshow.setText(_translate("MainWindow", "IP GOES HERE", None))
        self.groupBox_2.setTitle(_translate("MainWindow", "WIFI IP", None))
        self.wifiipshow.setText(_translate("MainWindow", "IP GOES HERE", None))
        self.groupBox_3.setTitle(_translate("MainWindow", "VPN IP", None))
        self.vpnipshow.setText(_translate("MainWindow", "IP GOES HERE", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.statusdisplaytab), _translate("MainWindow", "Status Display", None))
        self.label_7.setText(_translate("MainWindow", "Current Software Version:", None))
        self.label_8.setText(_translate("MainWindow", "016", None))
        self.groupBox_7.setTitle(_translate("MainWindow", "Software Version", None))
        self.checkforupdatebutton.setText(_translate("MainWindow", "Check For Update", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.softwareupdatetab), _translate("MainWindow", "Software Update", None))
        self.groupBox_8.setTitle(_translate("MainWindow", "Select Action", None))
        self.closemenubutton.setText(_translate("MainWindow", "Close Menu", None))
        self.iteratenewpunch.setText(_translate("MainWindow", "Iterate and Deploy New Punch!", None))
        self.cloneexistingpunchbutton.setText(_translate("MainWindow", "Clone Existing Punch!", None))
        self.restartmenubutton.setText(_translate("MainWindow", "Restart Menu", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.devmenutab), _translate("MainWindow", "Dev Menu", None))

from PyQt4 import QtWebKit

这是否有助于确定不应该做的事情?

2 个答案:

答案 0 :(得分:0)

您的closemenu功能需要成为您班级的一部分。

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.closemenubutton.clicked.connect(self.closemenu)

    # Notice this has been brought into the class
    def closemenu(self):
        sys.exit()

您没有提供您的UI文件,因此我无法执行完全测试,但我可以向您展示这是一个简单的示例:

此代码创建一个按钮:

导入系统 来自PyQt4导入QtGui,QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.closebutton = QtGui.QPushButton('Exit', self)
        self.closebutton.clicked.connect(self.exit_button)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.closebutton)

    def exit_button(self):
        sys.exit()

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

看起来像这样:

Example Button

单击该按钮时,应用程序将关闭,退出状态为0(一个干净的退出)

C:\Sandbox>python exit_button.py
C:\Sandbox>echo %ERRORLEVEL%
0

答案 1 :(得分:0)

无论底层问题是什么,我都可以通过切换到使用Eric IDE和Qt设计器并遵循本教程来解决问题:http://eric-ide.python-projects.org/tutorials/MiniBrowser/index.html以了解开发环境的好处&#34} 34;在我之前使用的基本上记事本的数量!

当你可以看到你附加的手柄时,定义动作会容易得多! 阅读此内容的其他新手 - 遵循该指南,它让我从面向对象的编程和GUI开始。