簡體   English   中英

Tkinter GUI 沒有響應

[英]Tkinter GUI does not respond

我是編程新手,我正在嘗試運行一個程序,該程序要求用戶提供一個目錄,他們會將文件移動到該目錄。

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

那么你到底在期待什么? 你的程序確實有響應,但它不應該對信息做任何事情。 嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM