【问题标题】:Make button execute a script使按钮执行脚本
【发布时间】:2020-02-08 18:43:15
【问题描述】:

我有一个用于组织文件夹的脚本。我想改进它,所以我正在尝试制作 GUI。我已经花了一个多星期的时间,但我找不到解决方案......你如何正确混合链接第二个和第三个脚本以将它们集成到主脚本中?那么,如何在一个可执行文件中编译三个文件?

这里是主脚本

#Introduction of the code
"""The aim of this script is to organize your download folder by placing files in a sub-folder regarding of their extension"""
import os
import shutil
import getpass

def dot_exe_dir_download(x,y): # x is the complete name of the file and y is the current folder
    destination=y+"Installer/" #destination of the file
    a=path_download_directory+x #recreate the path of the file
    #print(a)
    test=os.path.exists(destination) #check if the folder exist or not
    if test == True:
        shutil.move(a,destination) #move the file to the destination
    else:
        os.mkdir(destination) # create the folder that doesn't exist
        shutil.move(a,destination) #move the file to the destination
    print(x," has been moved to:",destination,)

def dot_rmskin_dir_download(x,y): # x is the complete name of the file and y is the current folder
    destination=y+"Skins/Rainmeter/" #destination of the file
    a=path_download_directory+x #recreate the path of the file
    #print(a)
    test=os.path.exists(destination) #check if the folder exist or not
    if test == True:
        shutil.move(a,destination) #move the file to the destination
    else:
        os.mkdir(destination) # create the folder that doesn't exist
        shutil.move(a,destination) #move the file to the destination
    print(x," has been moved to:",destination,)

def dot_fonts_dir_download(x,y): # x is the complete name of the file and y is the current folder
    destination=y+"Fonts/" #destination of the file
    a=path_download_directory+x #recreate the path of the file
    #print(a)
    test=os.path.exists(destination) #check if the folder exist or not
    if test == True:
        shutil.move(a,destination) #move the file to the destination
    else:
        os.mkdir(destination) # create the folder that doesn't exist
        shutil.move(a,destination) #move the file to the destination
    print(x," has been moved to:",destination,)

def dot_video_dir_download(x,y): # x is the complete name of the file and y is the current folder
    destination=path_raw+"Videos/Downloaded video's" #destination of the file
    a=path_download_directory+x #recreate the path of the file*
    #print(a)
    test=os.path.exists(destination) #check if the folder exist or not
    if test == True:
        shutil.move(a,destination) #move the file to the destination
    else:
        os.mkdir(destination) # create the folder that doesn't exist
        shutil.move(a,destination) #move the file to the destination
    print(x," has been moved to:",destination,)

def archives_dir_download(x,y): # x is the complete name of the file and y is the current folder
    destination=y+"Archives/" #destination of the file
    a=path_download_directory+x #recreate the path of the file
    #print(a)
    test=os.path.exists(destination) #check if the folder exist or not
    if test == True:
        shutil.move(a,destination) #move the file to the destination
    else:
        os.mkdir(destination) # create the folder that doesn't exist
        shutil.move(a,destination) #move the file to the destination
    print(x," has been moved to:",destination,)



#Main body

#Part 1 (Main)
USER_ID=r'{}'.format(getpass.getuser())
USER_ID_MANUEL=""
while True:
    yes_or_no=str(input('Is your download directory is C:/Users/'+ str(USER_ID) +'/Downloads ? \nwrite yes or no: '))
    if yes_or_no == "yes":
        path_raw="C:/Users/"+ str(USER_ID) +"/"
        path_download_directory="C:/Users/"+ str(USER_ID) +"/Downloads/"
        print("------------")
        break
    elif yes_or_no == "no":
        USER_ID_MANUEL=(str(input(r"Please write down your exact user ID:")))
        path_raw="C:/Users/"+ str(USER_ID) +"/"
        path_download_directory="C:/Users/"+ str(USER_ID_MANUEL) +"/Downloads/"
        path_download_directory.replace("\\" , "/" )
        print("------------")

        break
    else:
        print("That answer was not expected. Please write yes or no")
        print("------------")

counter=0


#Part 2 (in /Download)
file_list=os.listdir(path_download_directory) # in the  workspace directory "/Downloads"
#print(path_download_directory)# print the current workspace path
#print(file_list)
print("---------")

for file in file_list:
    file_Name, file_Extension = os.path.splitext(file)
    #print(file_Extension)

    if file_Extension == ".exe":
        dot_exe_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".msi":
        dot_exe_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".zip":
        archives_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".rar":
        archives_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".7z":
        archives_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".iso":
        archives_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".rmskin":
        dot_rmskin_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".ttf":
        dot_fonts_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".otf":
        dot_fonts_dir_download(file,path_download_directory)
        counter+=1
        print("---")

    if file_Extension == ".mp4":
        dot_video_dir_download(file,path_download_directory)
        counter+=1
        print("---")

if counter==0:
    print("It's already organized :)")
else:
    print("We have moved",counter,"files")

print("---------")

print("------------")

input("Press Enter to finish")

目标是当我点击一个按钮时,它会打开一个文件夹对话框。 于是我写了文件夹对话框程序:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 directory dialogs'
        self.left = 100
        self.top = 100
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.openDirDialog()
        #self.show()

    def openDirDialog(self):
        options = QFileDialog.Options()
        file = str(QFileDialog.getExistingDirectory(self, "Select Directory", options=options))
        if file:
            print(file)
            return file

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

我需要一个窗口,所以我编写了一个窗口:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_FileOrganizer(object):
    def setupUi(self, FileOrganizer):
        FileOrganizer.setObjectName("FileOrganizer")
        FileOrganizer.resize(800, 598)
        FileOrganizer.setAcceptDrops(False)
        FileOrganizer.setLayoutDirection(QtCore.Qt.LeftToRight)
        FileOrganizer.setAnimated(True)
        FileOrganizer.setDockNestingEnabled(False)
        FileOrganizer.setUnifiedTitleAndToolBarOnMac(False)

        self.centralwidget = QtWidgets.QWidget(FileOrganizer)
        self.centralwidget.setObjectName("centralwidget")
        self.Title = QtWidgets.QLabel(self.centralwidget)
        self.Title.setGeometry(QtCore.QRect(170, 10, 451, 61))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI")
        font.setPointSize(20)
        self.Title.setFont(font)
        self.Title.setAlignment(QtCore.Qt.AlignCenter)
        self.Title.setObjectName("Title")

        self.Selectyourfolderbutton = QtWidgets.QPushButton(self.centralwidget)
        self.Selectyourfolderbutton.setGeometry(QtCore.QRect(310, 90, 161, 41))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI")
        font.setPointSize(12)
        self.Selectyourfolderbutton.setFont(font)
        self.Selectyourfolderbutton.setAutoDefault(True)
        self.Selectyourfolderbutton.setDefault(True)
        self.Selectyourfolderbutton.setFlat(False)
        self.Selectyourfolderbutton.setObjectName("Selectyourfolderbutton")
        FileOrganizer.setCentralWidget(self.centralwidget)

        self.menubar = QtWidgets.QMenuBar(FileOrganizer)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        FileOrganizer.setMenuBar(self.menubar)

        self.statusbar = QtWidgets.QStatusBar(FileOrganizer)
        self.statusbar.setObjectName("statusbar")
        FileOrganizer.setStatusBar(self.statusbar)

        self.retranslateUi(FileOrganizer)
        QtCore.QMetaObject.connectSlotsByName(FileOrganizer)

        self.Selectyourfolderbutton.clicked.connect(self.click)
    def click(self):
        import FolderSelect

        self.ex = App()

    def retranslateUi(self, FileOrganizer):
        _translate = QtCore.QCoreApplication.translate
        FileOrganizer.setWindowTitle(_translate("FileOrganizer", "File-Organizer"))
        self.Title.setText(_translate("FileOrganizer", "Welcome into File-Organizer !"))
        self.Selectyourfolderbutton.setText(_translate("FileOrganizer", "Select your folder"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    FileOrganizer = QtWidgets.QMainWindow()
    ui = Ui_FileOrganizer()
    ui.setupUi(FileOrganizer)
    FileOrganizer.show()
    sys.exit(app.exec_())

感谢阅读。如果您发现任何可以改进的地方(甚至是问题),请告诉我! 同样的问题或/和评论,让我知道!

【问题讨论】:

    标签: python python-3.x pyqt pyqt5


    【解决方案1】:

    如果您想实现一个可由 CLI 或 GUI 使用的应用程序,那么您必须首先定义业务逻辑。在您的情况下,它是在预定结构下的特定目录的组织,因此 GUI、CLI 或任何界面应该只提供目录并执行业务逻辑。因此,您的项目必须进行重组以满足上述要求:

    lib.py

    import os
    import shutil
    
    
    def move_file_to_directory(file, destination):
        if not os.path.exists(destination):
            os.mkdir(destination)
        shutil.move(file, destination)
        print("{} has been moved to: {}".format(os.path.basename(file), destination))
    
    
    def organize_folder(folder):
        counter = 0
        for filename in os.listdir(folder):
            name, extension = os.path.splitext(filename)
            d = {
                ".exe": "Installer",
                ".msi": "Installer",
                ".zip": "Archives",
                ".rar": "Archives",
                ".7z": "Archives",
                ".iso": "Archives",
                ".rmskin": "Skins/Rainmeter/",
                ".ttf": "Fonts",
                ".otf": "Fonts",
                ".mp4": "Videos/Downloaded video's",
            }
            sub_path = d.get(extension)
    
            if sub_path is not None:
                destination = os.path.join(folder, sub_path)
                source = os.path.join(folder, filename)
                move_file_to_directory(source, destination)
                counter += 1
        if counter == 0:
            print("It's already organized :)")
        else:
            print("We have moved {} files".format(counter))
    

    如您所见,lib.py 的部分不执行任何操作,但它是一组允许您根据特定规则组织目录的函数。

    cli.py

    import getpass
    import os
    
    from lib import organize_folder
    
    
    def build_name_of_directory(id_):
        return os.path.join("C:/Users", id_, "Downloads")
    
    
    def main():
        USER_ID = r"{}".format(getpass.getuser())
        download_path = build_name_of_directory(USER_ID)
        while True:
            yes_or_no = input(
                "Is your download directory is {}? \nwrite yes or no: ".format(
                    download_path
                )
            )
    
            if yes_or_no in ("yes", "no"):
                if yes_or_no == "no":
                    custom_id = str(input(r"Please write down your exact user ID:"))
                    download_path = build_name_of_directory(custom_id)
                break
            else:
                print("That answer was not expected. Please write yes or no")
                print("------------")
        organize_folder(download_path)
    
    
    if __name__ == "__main__":
        main()
    

    在 cli.py 的情况下,这实现了用户根据用户 ID 指定目录的逻辑,并使用 lib.py 的功能。

    gui.py

    import threading
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    from lib import organize_folder
    
    
    class MainWindow(QtWidgets.QMainWindow):
        started = QtCore.pyqtSignal()
        finished = QtCore.pyqtSignal()
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.setWindowTitle(self.tr("File-Organizer"))
            font = QtGui.QFont()
            font.setFamily("Yu Gothic UI")
            font.setPointSize(20)
    
            self.title = QtWidgets.QLabel(
                self.tr("Welcome into File-Organizer !"), alignment=QtCore.Qt.AlignCenter
            )
            self.title.setFont(font)
    
            self.button = QtWidgets.QPushButton(self.tr("Select your folder"))
            font.setPointSize(12)
            self.button.setFont(font)
            self.button.setFixedSize(160, 40)
    
            self.progressbar = QtWidgets.QProgressBar()
            self.progressbar.setFixedSize(300, 40)
    
            central_widget = QtWidgets.QWidget()
            self.setCentralWidget(central_widget)
    
            lay = QtWidgets.QVBoxLayout(central_widget)
            lay.setContentsMargins(0, 10, 0, 0)
            lay.setSpacing(60)
            lay.addWidget(self.title)
            lay.addWidget(self.button, alignment=QtCore.Qt.AlignCenter)
            lay.addWidget(self.progressbar, alignment=QtCore.Qt.AlignCenter)
            lay.addStretch()
    
            self.resize(640, 480)
    
            self.button.clicked.connect(self.onClicked)
            self.started.connect(self.onStarted)
            self.finished.connect(self.onFinished)
    
        @QtCore.pyqtSlot()
        def onClicked(self):
            directory = QtWidgets.QFileDialog.getExistingDirectory(
                self,
                "Select Directory",
                QtCore.QStandardPaths.writableLocation(
                    QtCore.QStandardPaths.DownloadLocation
                ),
            )
            if directory:
                threading.Thread(
                    target=self.organize_folder, args=(directory,), daemon=True
                ).start()
    
        def organize_folder(self, directory):
            self.started.emit()
            organize_folder(directory)
            self.finished.emit()
    
    
        @QtCore.pyqtSlot()
        def onStarted(self):
            self.progressbar.setRange(0, 0)
    
        @QtCore.pyqtSlot()
        def onFinished(self):
            self.progressbar.setRange(0, 1)
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    

    gui.py 与 cli.py 一样,除了指示订购的进度外,仅向用户提供了一个接口,用于获取了解您要订购哪个目录的必要信息。由于应该移动的文件数量未知,因此该任务可能很繁重,因此必须在另一个线程中执行。

    结构:

    ├── cli.py
    ├── gui.py
    └── lib.py
    

    【讨论】:

    • 哇哦!什么答案!我正在寻找和研究它。
    • @O'Schell 它没有必要在“站点包”中,它位于 cli.py 和 gui.py 文件旁边就足够了。你是如何运行你的脚本的?
    • 再次感谢您的帮助!!祝你好运,玩得开心!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多