对于我的问题,我找到了很多不同的答案,但是似乎没人在为我工作。我正在尝试从Qt和Python设置GUI。 第一个问题是我必须使用:
from mainwindow import Ui_MainWindow
代替
from ui.mainwindow import Ui_MainWindow
因为似乎我无法从主文件夹读取到名为ui的子文件夹中。我只是绕过了它,将main.py移到ui文件夹中。 运行它的GUI,但指令self.setWindowTitle(“ ABC”)无效。
但是当我尝试不受欢迎的时候
self.ui.myOK.clicked.connect(self.onbc)
进入:
import sys
from qtpy import QtWidgets
from mainwindow import Ui_MainWindow
app = QtWidgets.QApplication(sys.argv)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("ABC")
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#self.ui.myOK.clicked.connect(self.onbc)
def onbc(self):
print("abc")
window = MainWindow()
window.show()
sys.exit(app.exec_())
我获得:
Traceback (most recent call last):
File "/Users/alessandro/Documents/Lavoro/Phyton/sviluppo/gui/ui/main.py", line 21, in <module>
window = MainWindow()
File "/Users/alessandro/Documents/Lavoro/Phyton/sviluppo/gui/ui/main.py", line 16, in __init__
self.ui.myOK.clicked.connect(self.onbc)
AttributeError: 'Ui_MainWindow' object has no attribute 'myOK'
我的主窗口文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui/mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# 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(227, 364)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
self.gridLayout.setContentsMargins(11, 11, 11, 11)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName("gridLayout")
self.label_6 = QtWidgets.QLabel(self.centralWidget)
self.label_6.setObjectName("label_6")
self.gridLayout.addWidget(self.label_6, 0, 0, 1, 1)
self.splitter = QtWidgets.QSplitter(self.centralWidget)
self.splitter.setOrientation(QtCore.Qt.Vertical)
self.splitter.setObjectName("splitter")
self.gridLayout.addWidget(self.splitter, 2, 2, 2, 1)
self.List = QtWidgets.QPushButton(self.centralWidget)
self.List.setObjectName("List")
self.gridLayout.addWidget(self.List, 6, 1, 1, 1)
self.StartX_2 = QtWidgets.QLineEdit(self.centralWidget)
self.StartX_2.setText("")
self.StartX_2.setObjectName("StartX_2")
self.gridLayout.addWidget(self.StartX_2, 0, 1, 1, 1)
self.label_5 = QtWidgets.QLabel(self.centralWidget)
self.label_5.setObjectName("label_5")
self.gridLayout.addWidget(self.label_5, 4, 0, 1, 1)
self.Elaborate = QtWidgets.QPushButton(self.centralWidget)
self.Elaborate.setObjectName("Elaborate")
self.gridLayout.addWidget(self.Elaborate, 6, 0, 1, 1)
self.StartY = QtWidgets.QLineEdit(self.centralWidget)
self.StartY.setObjectName("StartY")
self.gridLayout.addWidget(self.StartY, 3, 1, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralWidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.label_3 = QtWidgets.QLabel(self.centralWidget)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 3, 0, 1, 1)
self.StartX = QtWidgets.QLineEdit(self.centralWidget)
self.StartX.setObjectName("StartX")
self.gridLayout.addWidget(self.StartX, 1, 1, 1, 1)
self.label = QtWidgets.QLabel(self.centralWidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
self.myOk = QtWidgets.QPushButton(self.centralWidget)
self.myOk.setObjectName("myOk")
self.gridLayout.addWidget(self.myOk, 5, 0, 1, 2)
self.IncrX = QtWidgets.QLineEdit(self.centralWidget)
self.IncrX.setObjectName("IncrX")
self.gridLayout.addWidget(self.IncrX, 2, 1, 1, 1)
self.ElaborareAndList = QtWidgets.QPushButton(self.centralWidget)
self.ElaborareAndList.setObjectName("ElaborareAndList")
self.gridLayout.addWidget(self.ElaborareAndList, 7, 0, 1, 2)
self.Ydirection = QtWidgets.QLineEdit(self.centralWidget)
self.Ydirection.setInputMask("")
self.Ydirection.setObjectName("Ydirection")
self.gridLayout.addWidget(self.Ydirection, 4, 1, 1, 1)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 227, 22))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
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.label_6.setText(_translate("MainWindow", "Set Folder"))
self.List.setText(_translate("MainWindow", "List"))
self.label_5.setText(_translate("MainWindow", "Y direction"))
self.Elaborate.setText(_translate("MainWindow", "Elaborate"))
self.StartY.setText(_translate("MainWindow", "0.00"))
self.label_2.setText(_translate("MainWindow", "Set increment X"))
self.label_3.setText(_translate("MainWindow", "Set start Y"))
self.StartX.setText(_translate("MainWindow", "0.00"))
self.label.setText(_translate("MainWindow", "Set start X"))
self.myOk.setText(_translate("MainWindow", "Ok"))
self.IncrX.setText(_translate("MainWindow", "0.64"))
self.ElaborareAndList.setText(_translate("MainWindow", "Elaborate and List "))
self.Ydirection.setText(_translate("MainWindow", "1"))
有什么主意吗?我不知道该怎么办...