首页 > 解决方案 > Tkinter GUI 没有响应

问题描述

我是编程新手,我正在尝试运行一个程序,该程序要求用户提供他们将文件移动到的目录。

import tkinter as tk
from tkinter import filedialog, ttk

class Unzip():
    def __init__(self):
        #  initialising the screen name, size title
        root = tk.Tk()
        root.geometry('650x550')
        root.title("Move Files")
        root.resizable(0, 0)
        root.configure(bg='#002060')

        # Initialising the frame to insert our widget
        top_frame = tk.Frame(root, bg='#002060')
        button_top = tk.Frame(root, bg='#002060')
        button_bottom = tk.Frame(root, bg='#002060')
        footer_frame = tk.Frame(root, bg='#002060')

        top_frame.pack(side='top', fill='both')
        button_top.pack(side='top', fill='both')
        button_bottom.pack(side='top', fill='both')
        footer_frame.pack(side='bottom', fill='both')

        # Setting the title name
        label_title = tk.Label(top_frame, text='Move Files', font='Arial 36 bold', fg='#948a54',
                               bg='#002060', pady=60)
        label_title.pack(side='top')


# call button to get output file
        output_file = ttk.Button(button_bottom, text='Choose location to save files', width=25, command=self.output_path)
        output_file.pack(side='left', padx=(120, 10), pady=10)

        root.mainloop()

# Get output directory
    def output_path(self):
        self.output_file_dir = filedialog.askdirectory()


if __name__ == '__main__':
    Unzip()

问题是当您运行程序并尝试运行output_file按钮时,程序将没有响应。我决定使用 ,self因为我希望它可以被我想要创建的其他将使用目录的实例方法访问output_path

标签: pythontkinter

解决方案


那么你到底在期待什么?您的程序确实响应,但它不应该对信息做任何事情。尝试

def output_path(self):
    self.output_file_dir = filedialog.askdirectory()
    print(self.output_file_dir)

你看到会发生什么吗?也许此链接中的示例 2 可以帮助您:https ://www.programcreek.com/python/example/95888/tkinter.filedialog.askdirectory


推荐阅读