python - 加载目录后在 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))
解决方案
为第一个选项卡创建一个信号:
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))
注意:避免使用全局变量,除非您真的知道它们是什么以及它们是如何工作的,否则不应使用它们,当然也不应像您尝试的那样使用它们:两者x
都workdir
在其脚本范围内有效,因此尝试从另一个脚本访问或更改它们绝对不会有任何结果。
推荐阅读
- javascript - React JS循环文本并应用效果错误
- python - Firebase 嵌套索引规则
- php - Wordpress 和 F5 WAF - SSL 导致的无限循环
- javascript - var1 每加 1,var2 加 1
- javascript - MongoDB查询嵌套数组知道父id元素(JavaScript)
- javascript - 如何使用我的电子邮件别名作为 GSUITE 和 Nodemailer 的原始发件人?
- java - 连接到 localhost:5432 被拒绝。检查主机名和端口是否正确以及 postmaster 是否接受 TCP/IP 连接。2
- javascript - Javascript - 嵌套循环内的 Math.random 每次都返回相同的序列
- javascript - SvelteKit 开发服务器无法启动
- r - 通过原点绘制 geom_smooth/geom_spline 回归线