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

标签 python user-interface tkinter zip

各位程序员大家好,

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

  1. 要求用户(通过输入和“确定”按钮)输入工作文件夹的名称。结果->在系统桌面上创建新建的文件夹。
  2. 通过 TkFileDialog 选择一个 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。因此,类中的任何其他方法都无法访问它。如果您确实希望它可访问,那么您需要使用 self 关键字将其设置为类的属性。

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从字符串构造文件路径。

关于python - 使用 Tkinter 创建一个 GUI,用于创建文件夹并解压 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104872/

相关文章:

python - 创建以另一列值为条件的 pandas 列

user-interface - 很棒的首次运行体验

python - Python click 库中急切选项的非交互式确认

python - 通过触摸板滚动触发 CTRL KeyPress 事件

python - 当将 imaplib 与 cx_Freeze 一起使用时,获取 "module ' imaplib' 没有属性 'IMAP4_SSL'

python - 如何制作Python图像 'fit'这个数字?

python - Python 中的评级控制

c# - 桌面应用程序爬虫

python - 我的执行程序不工作

python - 在 Python 中无需重启应用程序即可检索 MySQL 数据库的新行