python - 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
。
解决方案
那么你到底在期待什么?您的程序确实响应,但它不应该对信息做任何事情。尝试
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
推荐阅读
- r - 从长到宽处理数据
- git - github:在下载(如 .zip 或 .tar.gz)中查找特定提交的哈希(标签)?
- r - 热图上的色标
- antd - Ant设计图表,如何在柱形图中显示重复名称的数据
- android - 在活动或片段中使用 viewmodel.myData = myData 是反模式吗
- laravel - 尝试在调用 Laravel Api 时访问 VueJs 上 null 类型值的数组偏移量
- python - scipy.sparse.csr_matrix 和 scipy.sparse.lil_matrix 有什么不同
- mqtt - 在 MQTT 规范中,为什么我们不能在订阅时为主题设置“别名”?
- timedelta - 如何在 Delta History Metadata 中填充 Delta IO 作业字段
- ruby-on-rails - Rails 迁移将时间更改为保留数据的日期时间