在python中访问USB驱动器以供初学者使用

时间:2014-10-22 13:45:42

标签: python pyusb

级别的学生正在进行计算的第二年项目,并决定尝试创建一个访问USB驱动器的实用程序,并返回不同文件和文件夹在GUI中占用USB空间的百分比值(代码用于GUI)。

from tkinter import *
from tkinter.ttk import *
def select():
    sf = "value is %s" % var.get()
    root.title(sf)
    # optional
    color = var.get()
    root['bg'] = color
root = Tk()

# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (1000, 600, 10, 10))
root.title("USB scanner")
frame1=Frame(root)
frame1.pack(side=TOP, anchor=W)
var = StringVar(root)
# initial value

var.set('select drive')
choices = ['', '', '', '','', '']
option = OptionMenu(frame1, var, *choices)
option.pack(side=LEFT)
button = Button(frame1, text="Scan selected drive", command=select)
button.pack(side=LEFT, padx=30, pady=30)

root.mainloop()

我已经安装了pyUSB-1.0.0a2和libopenusb-1.1.11我继续得到后端不可用错误我检查了DLL。如果我老实说,我不知道我做了多少事情,所以任何基本的帮助都表示赞赏

1 个答案:

答案 0 :(得分:0)

以下程序应该有助于您指明正确的方向:

from tkinter import *
from shutil import disk_usage

class Sizer(Frame):

    @classmethod
    def main(cls):
        NoDefaultRoot()
        root = Tk()
        root.geometry('{}x{}'.format(200, 100))
        root.resizable(False, False)
        root.title('Drive Information')
        widget = cls(root)
        widget.grid(row=0, column=0, sticky=NSEW)
        root.mainloop()

    def __init__(self, master=None, cnf={}, **kw):
        super().__init__(master, cnf, **kw)
        self.selection = StringVar(self)
        self.menu = OptionMenu(self, self.selection, 'A:\\', 'B:\\', 'C:\\')
        self.menu.grid(row=0, column=0, sticky=NSEW)
        self.button = Button(self, text='Scan the drive', command=self.scan)
        self.button.grid(row=1, column=0, sticky=NSEW)
        self.display = Label(self)
        self.display.grid(row=2, column=0, sticky=NSEW)

    def scan(self):
        disk = self.selection.get()
        try:
            information = disk_usage(disk)
        except FileNotFoundError:
            self.display['text'] = 'Disk {!r} cound not be found'.format(disk)
        else:
            self.display['text'] = 'Used: {}\nFree: {}'.format(
                information.used, information.free)

if __name__ == '__main__':
    Sizer.main()