首页 > 解决方案 > 加载目录后在 pyqt 中启用选项卡

问题描述

我正在设计一个 pyqt 应用程序,其中主布局有一些选项卡。第一个选项卡是用户加载默认目录 (tab1) 的位置。如果不加载目录,则无法启用所有其他选项卡。我一直在尝试在加载目录后启用选项卡,但到目前为止还没有成功。如果有人可以帮助我,那将意义重大。

我的文件夹结构是这样的:

ABC (Main folder)
    --files (sub folder)
        --tab1.py
        --tab2.py
    --home.py

在 home.py 中,我将 tab2 设置为默认禁用,当用户在 tab1 中加载目录时,我希望启用它。

这是我的三个页面的代码:

主页.py

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QLabel
from files.tab1 import Test
from files.tab2 import Tests

# Creating the main window
class App(QMainWindow):
    def __init__(self):
        super().__init__()

        self.tab_widget = MyTabWidget(self)
        self.setCentralWidget(self.tab_widget)
  
        self.show()
  
# Creating tab widgets
class MyTabWidget(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)
        
        self.tabs = QTabWidget()

        # Add tabs
        self.tabs.addTab(Test(), "Tab1")
        self.tabs.addTab(Tests(), "Tab2")
        # Add tabs to widget
        self.tabs.setTabEnabled(1,False)
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)
  
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

选项卡1.py

import sys
import os
from PyQt5.QtWidgets import (QWidget, 
                            QFileDialog, 
                            QHBoxLayout, 
                            QVBoxLayout, 
                            QPushButton, 
                            QLabel, 
                            QApplication)

#CREATING SUBCLASS OF WIDGET

class Test(QWidget):
    def __init__(self):
        super(Test, self).__init__()
        self.setWindowTitle('Home')

#layout
        horizontalLayout = QHBoxLayout()
        
        verticalLayout = QVBoxLayout()

        self.opendir = QPushButton("Select Working Directory")
        self.dirLabel = QLabel("Selected Directory: None")
        
        
        verticalLayout.addWidget(self.opendir)
        verticalLayout.addWidget(self.dirLabel)
        
        horizontalLayout.addLayout(verticalLayout,1)
         
        self.setLayout(horizontalLayout)

#SIGNALS
        
        self.opendir.clicked.connect(self.loadworkdir)
    
    workdir = os.path.expanduser("~")

    def loadworkdir(self):
        global workdir

        self.dialog = QFileDialog()
        workdir = self.dialog.getExistingDirectory(None, "Select Folder",os.path.expanduser("~"))
        if workdir == '':
            self.dirLabel.setText('Selected Directory: None')
        else:
            self.dirLabel.setText('Working Directory : {}'.format(workdir))

        return workdir
    
    def setupdir(self):
        global x
        x = workdir
        return x

选项卡2.py

import sys
import os
import glob
from PyQt5.QtWidgets import (QWidget, 
                            QFileDialog, 
                            QHBoxLayout, 
                            QVBoxLayout, 
                            QPushButton, 
                            QLabel, 
                            QApplication)
from files.tab1 import *

class Tests(QWidget):
    def __init__(self):
        super(Tests, self).__init__()

        self.setWindowTitle('Home')
        
        horizontalLayout = QHBoxLayout()
        
        verticalLayout = QVBoxLayout()

        self.opendir1 = QPushButton("Select files")
        self.shapeLabel = QLabel()
        
        verticalLayout.addWidget(self.opendir1)
        
        horizontalLayout.addLayout(verticalLayout,1)
         
        self.setLayout(horizontalLayout)
        
        self.opendir1.clicked.connect(self.loadfiles)

    def loadfiles(self):
        self.dir=Test.setupdir(self)
        os.chdir(self.dir)
        self.dialog = QFileDialog()
        self.fpath = self.dialog.getExistingDirectory(None, "Select Folder")
        csvcounter = len(glob.glob1(self.fpath,"*.csv"))
        self.shapeLabel.setText('CSV Files:{}'.format(csvcounter))

标签: pythonpython-3.xpyqtpyqt5

解决方案


为第一个选项卡创建一个信号:

from PyQt5.QtCore import *

class Test(QWidget):
    directoryLoaded = pyqtSignal(str)
    # ...
    def loadworkdir(self):
        global workdir

        self.dialog = QFileDialog()
        workdir = self.dialog.getExistingDirectory(None, "Select Folder",os.path.expanduser("~"))
        if workdir == '':
            self.dirLabel.setText('Selected Directory: None')
        else:
            self.dirLabel.setText('Working Directory : {}'.format(workdir))

        self.directoryLoaded.emit(workdir)

        return workdir

然后将其连接到启用主脚本中第二个选项卡的函数:

class MyTabWidget(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout(self)
        
        self.tabs = QTabWidget()

        # create a reference to the first tab, because we need to connect to its signal
        firstTab = Test()
        firstTab.directoryLoaded.connect(self.firstTabLoaded)

        # Add tabs
        self.tabs.addTab(firstTab, "Tab1")
        self.tabs.addTab(Tests(), "Tab2")
        # Add tabs to widget
        self.tabs.setTabEnabled(1, False)

        self.layout.addWidget(self.tabs)

        # not required: you already set it by adding "self" to the constructor
        # self.setLayout(self.layout)

    def firstTabLoaded(self, path):
        self.tabs.setTabEnabled(1, bool(path))

注意:避免使用全局变量,除非您真的知道它们是什么以及它们是如何工作的,否则不应使用它们,当然也不应您尝试的那样使用它们:两者xworkdir在其脚本范围内有效,因此尝试从另一个脚本访问或更改它们绝对不会有任何结果。


推荐阅读