I'm a beginner with PyQt and trying to update widgets in the main window with information that's given by the user in another dialog window.
this is my main window:
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
uic.loadUi('GUI_MainWindow.ui',self)
self.setWindowTitle("BR")
self.statusBar()
#save File Action
saveFile= QtWidgets.QAction('&Profil speichern',self)
saveFile.setShortcut('CTRL+S')
saveFile.setStatusTip('Save File')
saveFile.triggered.connect(self.file_save)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&Datei')
fileMenu.addAction(saveFile)
self.home()
def home(self):
self.dlg = self.findChild(QtWidgets.QPushButton, 'addfile')
self.dlg.clicked.connect(self.opensecondwindow)
self.show()
# this is the method that I want to call
def updatebez(self,widgetname,filename):
self.widg = self.findChild(QLabel, str(self.widgetname))
self.widg.setText(filename)
self.update()
print(self.widg.Text())
#calling the dialog window
def opensecondwindow(self):
self.sw = lesen(self)
self.sw.show()
def file_save(self):
name, _ = QtWidgets.QFileDialog.getSaveFileName(self, 'Save File',options=QFileDialog.DontUseNativeDialog)
file = open(name, 'w')
text = self.textEdit.toPlainText()
file.write(text)
file.close()
and this is the dialog window:
class lesen(QtWidgets.QDialog):
def __init__(self,parent):
global afl
global calendar
global fn
super(lesen, self).__init__(parent)
uic.loadUi('Kal.ui',self)
self.setWindowTitle("Parametrierung")
afl = self.findChild(QtWidgets.QLineEdit, 'Aufloesung')
calendar = self.findChild(QtWidgets.QCalendarWidget, 'Calendar')
self.addfile = self.findChild(QtWidgets.QPushButton, 'chooseFile')
slot = self.findChild(QtWidgets.QSpinBox, 'spinBox')
slotnr = slot.text()
widgetname = 'ZRName'+ slotnr
self.filename = self.findChild(QtWidgets.QLineEdit, 'Bez')
self.addfile.clicked.connect(self.updatebez(widgetname,self.filename))
self.addfile.clicked.connect(self.file_open)
def Datenanpassung(self, tempfile):
list=[]
zeitabstand = int(afl.text())*60
datum = calendar.selectedDate()
a = datetime.datetime(datum.year(),datum.month(),datum.day(),00,00,00)
for row in tempfile:
a = a + datetime.timedelta(0,zeitabstand)
datetimestr= str(a.date()) + ' ' + str(a.time())
row = [datetimestr, row[0]]
list.append(row)
return list
def file_open(self):
#Dateiauswahl
global name
global tempfile
global fn
tempfile = []
filters = ""
selected_filter = "csv or json (*.csv *.json)"
name, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Datei auswählen', filters, selected_filter,
options=QFileDialog.DontUseNativeDialog)
file = open(name, 'r', newline='')
for row in csv.reader(file):
tempfile.append(row)
self.anpassen = self.findChild(QtWidgets.QCheckBox, 'checkBox')
if self.anpassen.isChecked():
newfile = self.Datenanpassung(tempfile)
with open(os.path.basename(file.name)[:-4] +'_mit_DateTime.csv', 'w', newline='') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(newfile)
file = open(os.path.basename(file.name)[:-4] +'_mit_DateTime.csv', 'r', newline='')
reader = csv.DictReader( file, fieldnames = ( "DateTime","Wert"))
out = json.dumps(list(reader))
f = open( os.path.basename(file.name)[:-4] +'_mit_DateTime.json', 'w')
f.write(out)
else:
pass
Edit: the error I get is: (somehow it only pastes the first line as code)
Traceback (most recent call last):
File "D:/Data/Zeitreihen_1.0x2.py", line 141, in opensecondwindow
self.sw = lesen(self)
File "D:/Data/Zeitreihen_1.0x2.py", line 35, in __init__
self.addfile.clicked.connect(self.updatebez(widgetname,self.filename))
AttributeError: 'lesen' object has no attribute 'updatebez'
An exception has occurred, use %tb to see the full traceback.
SystemExit: 0
the goal behind the method updatez is to update a QLabel object in the main window from a text that the user typed in QLineEdit in the dialog window. Before adding the method and trying to call it in the Dialog window, everything was working fine. the error now shows up when i try to click on the button that shows the dialog window.
I know the best solution would be setting up a new class for a signal between both the main window and the dialog window but I couldn't get it right . Therefore I would like to know if it is possible to make the code do what it has to do without using a signal.
Thanks in advance, internet wizards!