我想从我的UI连接一个按钮到我的python代码。

时间:2018-05-17 07:58:29

标签: python qt pyqt5 designer

我首先尝试连接示例取消按钮,单击该按钮时将显示“Hello”。再说一遍,那只是一个样本。我只想知道如何将我的ui连接到id用于取消按钮的python代码。我希望有一个人可以帮助我。我是python的新手。

下面是代码:

saver = tf.train.Saver()

# Initialize a session so that we can run TensorFlow operations
with tf.Session() as session:

    # here is important, you need to load weights not initialize
    saver.restore(session, "logs/trained_model.ckpt")

    # then evaluate

1 个答案:

答案 0 :(得分:-1)

试一试:

from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.QtCore    import pyqtSlot

from A3AMainWindow import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.setupUi(self)

        self.pushButton.clicked.connect(self.on_click)

    @pyqtSlot()
    def on_click(self):
        QMessageBox.information(self, "Announcement.","Hello")

if __name__ == "__main__":
    import sys

    app    = QApplication(sys.argv)
    window = MainWindow() 
    window.show()
    sys.exit(app.exec_())

A3AMainWindow.py

# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(134, 110, 121, 71))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 400, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

enter image description here