我有2个py文件:my.py(这是我从ui创建的py-我有窗口和输入行,按钮的脚本)和vat.py(这是第二个脚本,单击pushButton_2时需要启动) 。我想从增值税脚本运行所有行,但my.py需要将一些变量传输到vat.py。这是一个问题-我该怎么做?
my.py
# -*- coding: utf-8 -*-
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!
#import vat
import os
import openpyxl
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDialog, QInputDialog
global glob
glob = "empty"
class Ui_MainWindow(QtWidgets.QWidget):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(633, 392)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icon01.webp"), QtGui.QIcon.Selected, QtGui.QIcon.On)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 40, 561, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(590, 40, 25, 19))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.browsefiles)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 20, 141, 16))
font = QtGui.QFont()
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.mdiArea.setGeometry(QtCore.QRect(10, 10, 611, 81))
self.mdiArea.setObjectName("mdiArea")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(20, 110, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
**self.pushButton_2.clicked.connect(self.start_app)**
self.mdiArea.raise_()
self.lineEdit.raise_()
self.pushButton.raise_()
self.label.raise_()
self.pushButton_2.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 633, 21))
self.menubar.setObjectName("menubar")
self.menuKonwerter = QtWidgets.QMenu(self.menubar)
self.menuKonwerter.setObjectName("menuKonwerter")
MainWindow.setMenuBar(self.menubar)
self.menubar.addAction(self.menuKonwerter.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def browsefiles(self):
path = "~"
full_path = os.path.expanduser(path)
filename = QFileDialog.getOpenFileName(None, "Plik excel do zaimportowania", full_path, "Excel (*.xls *.xlsx)")
filepath = filename[0]
self.lineEdit.setText(filepath)
def start_app(self):
filepath = self.lineEdit.text()
glob = filepath
try:
import vat
except:
infoBox = QMessageBox() ##Message Box that doesn't run
infoBox.setIcon(QMessageBox.Question)
infoBox.setText("Błąd pliku!")
infoBox.setInformativeText("Użyj rozszerzenia .xlsx.")
infoBox.setWindowTitle("Załadowano błędny plik")
#infoBox.setDetailedText("Detailed Text")
#infoBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
infoBox.setEscapeButton(QMessageBox.Close)
infoBox.exec()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Spradzenie podatników VAT"))
self.pushButton.setText(_translate("MainWindow", "..."))
self.pushButton_2.setText(_translate("MainWindow", "Uruchom"))
self.label.setText(_translate("MainWindow", "Plik wejściowy (excel):"))
self.menuKonwerter.setTitle(_translate("MainWindow", "Konwerter"))
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_())
vat.py
from my import *
print(glob)
#imp = Ui_MainWindow.setupUi.__getattribute__(lineEdit.text()) - dosent work
一切都很好,但是当我运行my.py并单击button2(如您所见,我正在从行编辑获取路径)时,此操作无效。我在vat.py脚本中看到“ glob = empty”。当然,vat.py文件具有更多功能和执行某些操作,但是我需要从vat.py文件进行所有步骤(但是我需要传递一些变量。当vat完成所有步骤时,是否还可以将vat反馈给我?TIA
您需要在vat.py中创建一个函数,然后将代码移入其中。这样,您可以将功能导入脚本的顶部,但仅在需要时使用call。您也可以多次调用它(与导入不同)。当然,您可以将参数传递给函数。
vat.py看起来像:
def vat(glob):
print(glob)
然后在my.py中,您可以执行以下操作:
from vat import vat
vat(glob)
这不完全是我想要的。在vat.py中,我有一些功能,但也有一段代码(无功能-有些用于执行某些操作)。当我在my.py中导入增值税时,这段代码正在启动-但只有在单击button2时,它才应该启动。