[Python] Displaying a message while formatting a thumb drive

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I've been struggling with this code which displays a message while formatting a thumb drive. The code has been cobbled together using various snippets from StackOverflow. While it does what its supposed to there are problems. The first is that it finishes with an Access Violation (0x0000005). The second is that when I try to set the font I get a runtime error "main thread is not in main loop" I believe that both these problems relate to threading in the code but don't know how to fix it. Any help would be greatly appreciated. (the flag done_format is there to prevent the messagebox from displaying twice. This is Kludgy and I expect it may be related to the other problems)

    from PyQt5.QtWidgets import QMessageBox, QApplication, QPushButton
    import sys
    from ctypes import *
    import threading, time

    import tkinter
    from tkinter import *
    import tkinter.font as tkFont

    REMOVABLE = 2

    def showMessageBox(options, text, default):
    msgBox = QMessageBox()
    msgBox.setIcon(QMessageBox.Critical)
    msgBox.setText(text)
    msgBox.setStandardButtons(options)
    msgBox.setDefaultButton(default)
    return msgBox.exec()

    class Format():
    done_format = False

    def __init__(self):
    self.retval = 0
    self.done_format = False

    def format_callback(self, command, modifier, arg):
    self.done_format = True
    return True

    def format_drive(self, Drive, Format, Title):

    self.retval = 0

    fm = windll.LoadLibrary('fmifs.dll')
    FMT_CB_FUNC = WINFUNCTYPE(c_int, c_int, c_int, c_void_p)
    FMIFS_UNKNOWN = 0

    self.processing_format(Drive[0],
    'FORMATTING DRIVE ' + Drive[0],
    fm.FormatEx(c_wchar_p(Drive), FMIFS_UNKNOWN, c_wchar_p(Format),
    c_wchar_p(Title), True, c_int(0), FMT_CB_FUNC(self.format_callback)))

    def processing_format(self, drive, text, function):
    import win32file
    window = tkinter.Tk()

    w = 500
    h = 200
    ws = window.winfo_screenwidth()
    hs = window.winfo_screenheight()
    x = (ws / 2) - (w / 2)
    y = (hs / 2) - (h / 2)
    window.geometry('%dx%d+%d+%d' % (w, h, x, y))

    #my_font = tkFont.Font(family='Arial', size=14)
    #label = Label(window, text=text, font=my_font).pack()
    label = Label(window, text=text).pack()
    done = []

    def call():
    result = function()
    done.append(result)

    app = QApplication(sys.argv)
    t = win32file.GetDriveType(drive + ':')
    if t != REMOVABLE:
    retval = showMessageBox(QMessageBox.Ok, 'Not a USB Drive', QAMessageBox.Ok)
    self.retval = -3
    return

    if not self.done_format:
    retval = showMessageBox(QMessageBox.No | QMessageBox.Yes,
    'ABOUT TO FORMAT DRIVE ' + Drive[0] + '\nOkay to Proceed',
    QMessageBox.No)
    if retval != QMessageBox.Yes:
    self.retval = -2
    return

    else: return

    thread = threading.Thread(target = call)
    thread.start()

    while thread.is_alive():
    window.update()
    time.sleep(0.001)

    if __name__ == "__main__":
    Drive = 'E:\\'
    Fmat = 'FAT32'
    Title = 'Formatting'
    proc = Format()

    proc.processing_format(Drive[0], 'FORMATTING DRIVE '+ Drive[0],
    lambda: proc.format_drive(Drive, Fmat, Title))
    print(proc.retval)

    Continue reading...

Compartilhe esta Página