我使用PyQT设计器创建了一个ui并将其转换为.py文件,然后粘贴以前编写为命令行脚本的代码,该脚本将.mp3文件(该地址写入lineEdit)拆分为块(其中的数字被读取)来自spinBox)。当试图运行代码时,我得到关于def函数内部代码行的FileNotFoundError。只有按下按钮时才能运行?我确定在按下按钮之前不应该执行代码,因此在显示UI并按下按钮之前不会出现错误代码。
错误代码:
Traceback (most recent call last):
File "C:/Users/Kacper/Desktop/Apki/AtomSplitter/untitled1.py", line 95, in <module>
ui.setupUi(MainWindow)
File "C:/Users/Kacper/Desktop/Apki/AtomSplitter/untitled1.py", line 66, in setupUi
self.pushButton.clicked.connect(atomsplitter(self, self.lineEdit.text(), self.spinBox.value()))
File "C:/Users/Kacper/Desktop/Apki/AtomSplitter/untitled1.py", line 18, in atomsplitter
Zawartosc = open(AdresPliku, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: ''
源代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.8
#
# WARNING! All changes made in this file will be lost!
import binascii, os
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(516, 191)
def atomsplitter(self, AdresPliku, Czesci):
NazwaPliku = os.path.basename(AdresPliku)
Zawartosc = open(AdresPliku, 'rb')
Zawartosc = Zawartosc.read()
Zawartosc = (binascii.hexlify(Zawartosc))
PoczatekIndeks = Zawartosc.find(b"fffb")
PoczatekIndeks = PoczatekIndeks
Poczatek = Zawartosc[0:PoczatekIndeks]
Koniec = Zawartosc[len(Zawartosc) - 256:]
Klatki = Zawartosc[PoczatekIndeks:len(Zawartosc) - 256]
Czesci = int(Czesci)
Dlugosc = int(len(Klatki) / Czesci)
Czesc = [0]
Indeks = [0]
for x in range(1, Czesci + 1):
Indeks.append(Zawartosc.find(b"fffb", Dlugosc * x))
Czesc.append(Klatki[Indeks[x - 1]:Indeks[x]])
strin = Czesc[x]
print(strin[len(strin) - 20:])
# if x == Czesci + 1:
# Czesc[x] = (Klatki[Indeks[x - 1]:])
# print("Czesc" + str(x) + " " + str(len(Czesc[x])))
CzescZawartosc = Poczatek + Czesc[x] + Koniec
CzescZawartosc = binascii.unhexlify(CzescZawartosc)
CzescPlik = open(NazwaPliku[:len(NazwaPliku) - 4] + str(x) + NazwaPliku[len(NazwaPliku) - 4:], "wb")
CzescPlik.write(CzescZawartosc)
CzescPlik.close()
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(20, 60, 71, 31))
self.textBrowser.setFocusPolicy(QtCore.Qt.NoFocus)
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_2.setGeometry(QtCore.QRect(20, 100, 91, 31))
self.textBrowser_2.setFocusPolicy(QtCore.Qt.NoFocus)
self.textBrowser_2.setObjectName("textBrowser_2")
self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox.setGeometry(QtCore.QRect(130, 100, 61, 31))
self.spinBox.setProperty("value", 2)
self.spinBox.setObjectName("spinBox")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(400, 140, 91, 31))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(120, 60, 271, 31))
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.pushButton.clicked.connect(atomsplitter(self, self.lineEdit.text(), self.spinBox.value()))
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.textBrowser_2, self.spinBox)
MainWindow.setTabOrder(self.spinBox, self.textBrowser)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Adres pliku:</p></body></html>"))
self.textBrowser_2.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Liczba części:</p></body></html>"))
self.spinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Liczba części</p></body></html>"))
self.pushButton.setText(_translate("MainWindow", "Split!"))
self.lineEdit.setPlaceholderText(_translate("MainWindow", "C:\\\\Users\\\\"))
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_())
我所做的唯一编辑是atomsplitter函数,导入os和binascii,以及在按钮和atomsplitter函数之间添加连接。
对不起,如果有些事情难以理解,英语不是我的第一语言。
答案 0 :(得分:0)
当您将信号连接到某个功能时,您必须通过名称及其调用来执行此操作,即myslot
而不是myslot()
。但您也可以使用lambda
。
变化:
self.pushButton.clicked.connect(atomsplitter(self, self.lineEdit.text(), self.spinBox.value()))
为:
self.pushButton.clicked.connect(lambda: atomsplitter(self, self.lineEdit.text(), self.spinBox.value()))