使用 Tkinter 创建一个 GUI,用于创建一个文件夹并解包一个 ZIP 文件

Using Tkinter to create a GUI that creates a folder and unwrap a ZIP file

各位程序员大家好,

我正在尝试使用 Tkinter 制作 GUI。这是我第一次使用 Tkinter,我 运行 遇到了一些问题。脚本和 GUI 应执行以下操作:

  1. 要求用户(通过条目和 'ok' 按钮)输入工作文件夹的名称。结果 -> 在系统桌面上创建新创建的文件夹。
  2. 通过 TkFileDialog
  3. Select 一个 OWL 文件(这是一个 zip 文件)。 结果 -> 在步骤 1 中创建的文件夹中解包所选的 zip 文件。

到目前为止我使用在线教程编写的脚本:

    import Tkinter
    import Tkconstants
    import tkFileDialog
    import zipfile
    from Tkinter import *
    import os

    class TkFileDialogExample(Tkinter.Frame):

      def __init__(self, root):

        Tkinter.Frame.__init__(self, root)

        root.configure(background='lightgrey')
        root.wm_title("Audit tool: Thickness of pavement")
        root.geometry('{}x{}'.format(500, 500))

        Label_1 = Message(root, text="Step 1. Please fill in the name of the output folder and click on 'create'. The output folder will be created in the desktop folder:", width=380,)
        Label_1.grid(row=1, columnspan=5)

        Entry1 = Entry(root)
        Entry1.grid(row=2, sticky=E)

        folder_location = '~/Desktop/' + Entry1.get()

        def createname():

          return os.mkdir(os.path.expanduser(folder_location))

        button_1 = Button(root, text="Create", command=createname)
        button_1.grid(row=2, column =1, sticky=W)

        Label_3 = Message(root, text="Step 2. Please select the OWL file:", width=300,)
        Label_3.grid(row=5, sticky=W)

        button_2 = Button(self, text='Click here to select the OWL file', command=self.askopenfilename)
        button_2.grid(row=4,column=1, sticky=W)

        self.file_opt = options = {}
        options['defaultextension'] = '.owl'
        options['filetypes'] = [('all files', '.*'), ('owl files', '.owl')]
        options['initialdir'] = 'C:\'
        options['initialfile'] = 'Title_of_OWL-file.ccr'
        options['parent'] = root
        options['title'] = 'This is a title'

        self.dir_opt = options = {}
        options['initialdir'] = 'C:\'
        options['mustexist'] = False
        options['parent'] = root

      def askopenfile(self):

        return tkFileDialog.askopenfile(mode='r', **self.file_opt)

      def askopenfilename(self):

        filename = tkFileDialog.askopenfilename(**self.file_opt)
        zip_ref = zipfile.ZipFile(filename, 'r')

        if filename:
          return zip_ref.extractall(folder_location)

    if __name__=='__main__':
      root = Tkinter.Tk()
      TkFileDialogExample(root).grid()
      root.mainloop()

问题可能出在第三次使用'folder_location'。由于我对 Python 语言比较陌生,我似乎找不到解决这个问题的方法。

感谢您的帮助和时间!

真诚的,

鲁本·范德海登

问题是您在 TkFileDialogExample.__init__ 方法的局部范围内只定义了变量 folder_location 。因此,您的 class 的任何其他方法都无法访问它。如果您 希望它可以访问,那么您需要使用 self 关键字将其设置为 class 的属性。

def __init__(self, root):

    # Stuff

    self.folder_location = os.path.join('~', 'Desktop', Entry1.get())

然后您可以从您的 TkFileDialogExample.askopenfilename 方法访问它:

def askopenfilename(self):

    filename = tkFileDialog.askopenfilename(**self.file_opt)
    zip_ref = zipfile.ZipFile(filename, 'r')

    if filename:
      return zip_ref.extractall(self.folder_location)

旁注:一般情况下,最好使用os.path.join从字符串构造文件路径。