I'm building a media player with PyQt5 in python and I want to access variables from my QMainWindow class. Currently I have two classes - one is the main window (QMainWindow) and the other is a QWidget Window so called my export manager.
Some basic code of my structure
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
# create gui
# some basic functions
# A function for opening a video with selection of file in explorer
def OpenVideo1(self, filename1=None):
file_filter = 'Video file (*.mp4 *.avi *wmv);'
filename1 = QFileDialog.getOpenFileName(
parent = self,
caption='Select a video file',
directory = os.getcwd(),
filter = file_filter,
initialFilter='Video file (*.mp4 *.avi *wmv)'
)[0]
....
# On button click the export manager window opens
def OpenExportManager(self, checked):
self.Exp = ExportManager()
self.Exp.show()
class ExportManager(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# Some gui functions etc...
def exportvideo
# Here I want to excecute some commands for export one video
# Therefore I need variables from the MainWindow - in this case filename1 and others
if __name__ == "__main__":
app = QApplication(sys.argv)
Main = MainWindow()
Main.show()
My question is now: How can I access the MainWindow variable filename1 from my ExportManager class? And how can I access methods the same way?
I tried already with signal/slots from pyqt5 or some class instances but I'm stuck...
Thanks very much for your help!
PyQt5 signals and slots but I got stuck.
CodePudding user response:
It's a point about Object-Oriented Programming more than about GUI.
You could keep track of the filename1
variable by making it a member of the MainWindow
class.
In MainWindow.init()
you could add :
self.chosen_filename: str = None
Then you assign it at the end of the MainWindow.OpenVideo()
method
self.chosen_filename = filename1
When you instantiate the ExportManager
object, you can pass the filename as a constructor argument.
def __init__(self, parent=None, filename=None)
...