如何在不同的QTabWidget上显示不同的目录
How to display different directories on different QTabWidget
我不能 运行 多个函数来设置不同选项卡的布局。
在我的代码中只有最后一个 def 函数 运行s 即使我交换了他们的位置仍然只有最后一个 def 即 运行s.
我尝试添加另一个函数来检查 (def_one),它起作用了,但仍然只有一个(最后一个)选项卡函数 运行s。
这是我的代码。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import os
import shutil
import DiskCleaner_ui
class main(QMainWindow, DiskCleaner_ui.Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
systray_icon = QIcon("s.png")
self.systray = QSystemTrayIcon(systray_icon, self)
menu = QMenu()
restore = QAction("Restore", self)
close = QAction("Close", self)
menu.addActions([restore, close])
self.systray.setContextMenu(menu)
self.systray.show()
self.systray.showMessage("DC", "Started...", QSystemTrayIcon.Information)
close.triggered.connect(self.close)
self.treeView = QTreeView()
self.fileSystemModel = QFileSystemModel(self.treeView)
self.fileSystemModel.setReadOnly(False)
self.root = self.fileSystemModel.setRootPath(r'C:\Users\Black Laptop\Desktop')
self.treeView.setModel(self.fileSystemModel)
self.treeView.setRootIndex(self.root)
self.treeView.setSortingEnabled(True)
self.Delete_Button = QPushButton("Clear Files")
self.Delete_Button.setFixedSize(90, 30)
self.Layout = QVBoxLayout(self)
self.Layout.addWidget(self.treeView)
self.Layout.addWidget(self.Delete_Button)
self.Duplicate() #doesnt run
self.Unused() #doesnt run
self.Temp() #last position : runs (even if i interchange functions between them
self.one()# to check : runs
def Temp(self):
self.Temp_Tab.setLayout(self.Layout)
self.Delete_Button.clicked.connect(self.Clear_Temp_Files)
def Duplicate(self):
self.Duplicate_Tab.setLayout(self.Layout)
def Unused(self):
self.UnUsed_Tab.setLayout(self.Layout)
def one(self):
print('self.one() works...')
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Exit', "Are you sure you want to exit?", QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def scan(self):
if os.listdir(r'C:\Users\Black Laptop\Desktop\Py1') == []:
self.systray.showMessage("DC", "Empty...", QSystemTrayIcon.Information)
else:
return
def Clear_Temp_Files(self):
self.scan()
with os.scandir(r'C:\Users\Black Laptop\Desktop\Py1') as entries:
for entry in entries:
if entry.is_file() or entry.is_symlink():
os.remove(entry.path)
self.systray.showMessage("DC", "Temporary files/folders cleared", QSystemTrayIcon.Information)
elif entry.is_dir():
shutil.rmtree(entry.path)
self.systray.showMessage("DC", "Temporary files/folders cleared", QSystemTrayIcon.Information)
if __name__ == '__main__':
app = QApplication(sys.argv)
H = main()
H.show()
app.exec_()
这里是 DiskCleaner_ui
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'DiskCleaner.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(594, 341)
#MainWindow.setStyleSheet("background-color: rgb(81, 81, 81);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setStyleSheet("background-color: rgb(189, 189, 189);\n"
"alternate-background-color: rgb(44, 44, 44);\n"
"border-color: rgb(185, 185, 185);")
self.tabWidget.setTabShape(QtWidgets.QTabWidget.Rounded)
self.tabWidget.setObjectName("tabWidget")
self.Temp_Tab = QtWidgets.QWidget()
self.Temp_Tab.setObjectName("Temp_Tab")
self.tabWidget.addTab(self.Temp_Tab, "")
self.Duplicate_Tab = QtWidgets.QWidget()
self.Duplicate_Tab.setObjectName("Duplicate_Tab")
self.tabWidget.addTab(self.Duplicate_Tab, "")
self.UnUsed_Tab = QtWidgets.QWidget()
self.UnUsed_Tab.setObjectName("UnUsed_Tab")
self.tabWidget.addTab(self.UnUsed_Tab, "")
self.verticalLayout.addWidget(self.tabWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Disk Cleaner"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.Temp_Tab), _translate("MainWindow", "Temp Files"))
self.tabWidget.setTabToolTip(self.tabWidget.indexOf(self.Temp_Tab), _translate("MainWindow", "Temporary files..."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.Duplicate_Tab), _translate("MainWindow", "Duplicate files"))
self.tabWidget.setTabToolTip(self.tabWidget.indexOf(self.Duplicate_Tab), _translate("MainWindow", "Duplicate Files..."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.UnUsed_Tab), _translate("MainWindow", "UnUsed files"))
self.tabWidget.setTabToolTip(self.tabWidget.indexOf(self.UnUsed_Tab), _translate("MainWindow", "Unused Files for a peeriod of time..."))
谢谢...
我通过为每个选项卡创建新的文件系统模型和根目录来解决问题
我不能 运行 多个函数来设置不同选项卡的布局。
在我的代码中只有最后一个 def 函数 运行s 即使我交换了他们的位置仍然只有最后一个 def 即 运行s.
我尝试添加另一个函数来检查 (def_one),它起作用了,但仍然只有一个(最后一个)选项卡函数 运行s。
这是我的代码。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import os
import shutil
import DiskCleaner_ui
class main(QMainWindow, DiskCleaner_ui.Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
systray_icon = QIcon("s.png")
self.systray = QSystemTrayIcon(systray_icon, self)
menu = QMenu()
restore = QAction("Restore", self)
close = QAction("Close", self)
menu.addActions([restore, close])
self.systray.setContextMenu(menu)
self.systray.show()
self.systray.showMessage("DC", "Started...", QSystemTrayIcon.Information)
close.triggered.connect(self.close)
self.treeView = QTreeView()
self.fileSystemModel = QFileSystemModel(self.treeView)
self.fileSystemModel.setReadOnly(False)
self.root = self.fileSystemModel.setRootPath(r'C:\Users\Black Laptop\Desktop')
self.treeView.setModel(self.fileSystemModel)
self.treeView.setRootIndex(self.root)
self.treeView.setSortingEnabled(True)
self.Delete_Button = QPushButton("Clear Files")
self.Delete_Button.setFixedSize(90, 30)
self.Layout = QVBoxLayout(self)
self.Layout.addWidget(self.treeView)
self.Layout.addWidget(self.Delete_Button)
self.Duplicate() #doesnt run
self.Unused() #doesnt run
self.Temp() #last position : runs (even if i interchange functions between them
self.one()# to check : runs
def Temp(self):
self.Temp_Tab.setLayout(self.Layout)
self.Delete_Button.clicked.connect(self.Clear_Temp_Files)
def Duplicate(self):
self.Duplicate_Tab.setLayout(self.Layout)
def Unused(self):
self.UnUsed_Tab.setLayout(self.Layout)
def one(self):
print('self.one() works...')
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Exit', "Are you sure you want to exit?", QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def scan(self):
if os.listdir(r'C:\Users\Black Laptop\Desktop\Py1') == []:
self.systray.showMessage("DC", "Empty...", QSystemTrayIcon.Information)
else:
return
def Clear_Temp_Files(self):
self.scan()
with os.scandir(r'C:\Users\Black Laptop\Desktop\Py1') as entries:
for entry in entries:
if entry.is_file() or entry.is_symlink():
os.remove(entry.path)
self.systray.showMessage("DC", "Temporary files/folders cleared", QSystemTrayIcon.Information)
elif entry.is_dir():
shutil.rmtree(entry.path)
self.systray.showMessage("DC", "Temporary files/folders cleared", QSystemTrayIcon.Information)
if __name__ == '__main__':
app = QApplication(sys.argv)
H = main()
H.show()
app.exec_()
这里是 DiskCleaner_ui
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'DiskCleaner.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(594, 341)
#MainWindow.setStyleSheet("background-color: rgb(81, 81, 81);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setStyleSheet("background-color: rgb(189, 189, 189);\n"
"alternate-background-color: rgb(44, 44, 44);\n"
"border-color: rgb(185, 185, 185);")
self.tabWidget.setTabShape(QtWidgets.QTabWidget.Rounded)
self.tabWidget.setObjectName("tabWidget")
self.Temp_Tab = QtWidgets.QWidget()
self.Temp_Tab.setObjectName("Temp_Tab")
self.tabWidget.addTab(self.Temp_Tab, "")
self.Duplicate_Tab = QtWidgets.QWidget()
self.Duplicate_Tab.setObjectName("Duplicate_Tab")
self.tabWidget.addTab(self.Duplicate_Tab, "")
self.UnUsed_Tab = QtWidgets.QWidget()
self.UnUsed_Tab.setObjectName("UnUsed_Tab")
self.tabWidget.addTab(self.UnUsed_Tab, "")
self.verticalLayout.addWidget(self.tabWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Disk Cleaner"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.Temp_Tab), _translate("MainWindow", "Temp Files"))
self.tabWidget.setTabToolTip(self.tabWidget.indexOf(self.Temp_Tab), _translate("MainWindow", "Temporary files..."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.Duplicate_Tab), _translate("MainWindow", "Duplicate files"))
self.tabWidget.setTabToolTip(self.tabWidget.indexOf(self.Duplicate_Tab), _translate("MainWindow", "Duplicate Files..."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.UnUsed_Tab), _translate("MainWindow", "UnUsed files"))
self.tabWidget.setTabToolTip(self.tabWidget.indexOf(self.UnUsed_Tab), _translate("MainWindow", "Unused Files for a peeriod of time..."))
谢谢...
我通过为每个选项卡创建新的文件系统模型和根目录来解决问题