我正在用 python 中的 PyQt5 構建一個媒體播放器,我想從我的 QMainWindow 類中訪問變數。目前我有兩個類 - 一個是主視窗(QMainWindow),另一個是 QWidget 視窗,稱為我的匯出管理器。
我的結構的一些基本代碼
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()
我現在的問題是:如何從我的 ExportManager 類訪問 MainWindow 變數 filename1?我怎樣才能以同樣的方式訪問方法?
我已經嘗試過使用來自 pyqt5 或某些類實體的信號/插槽,但我被卡住了......
非常感謝您的幫助!
PyQt5 信號和插槽,但我卡住了。
uj5u.com熱心網友回復:
這是關于面向物件編程的一點,而不是關于 GUI。
filename1您可以通過使其成為類的成員來跟蹤變數MainWindow。
在MainWindow.init()你可以添加:
self.chosen_filename: str = None
MainWindow.OpenVideo()然后在方法結束時分配它
self.chosen_filename = filename1
實體化ExportManager物件時,可以將檔案名作為建構式引數傳遞。
def __init__(self, parent=None, filename=None)
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529217.html
上一篇:gridview的所有按鈕都應該在顫振中適合其父容器
下一篇:如何使用底部導航欄更改頁面而不會在構建期間呼叫“FlutterErrorsetState()或markNeedsBuild()”。
