简体   繁体   English

Python tkinter askopenfilename 对我有用,现在没有响应

[英]Python tkinter askopenfilename was working for me and is now not responding

I'm developing a program with Python and tkinter that begins with the user adding a text file from their directory.我正在使用 Python 和 tkinter 开发一个程序,该程序从用户从他们的目录中添加一个文本文件开始。 I had built a GUI with tkinter that provided a button to be pressed and a popup window for them to select their file--it was working fine, and then suddenly when I tried to run it it would begin "Not Responding" when I pressed the button meant to launch the popup window.我用 tkinter 构建了一个 GUI,它提供了一个可供按下的按钮和一个供他们选择文件的弹出窗口——它工作正常,然后突然当我尝试运行它时,它会在我按下时开始“无响应”用于启动弹出窗口的按钮。

I am running Python 3.7.3 on Windows 10 in Jupyter notebooks;我在 Jupyter 笔记本中的 Windows 10 上运行 Python 3.7.3; the tkinter version is 8.6. tkinter 版本是 8.6。 I have 8 GB of RAM but I'm not using more than 80% of it.我有 8 GB 的内存,但我没有使用超过 80% 的内存。

I've tried looking at some similar Stack Overflow questions like the ones here and here:我试过查看一些类似的 Stack Overflow 问题,例如此处和此处的问题:

windows thinks tkinter is not responding Windows 认为 tkinter 没有响应

Python tkinter askopenfilename not responding Python tkinter askopenfilename 没有响应

Trying askopenfilenames() didn't work;尝试 askopenfilenames() 没有用; neither did adding root.update() or %gui tk.也没有添加 root.update() 或 %gui tk。

Here is the code I'm working with:这是我正在使用的代码:

import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import *

root = Tk()

topFrame=Frame(root)
topFrame.pack()

middleFrame=Frame(root, width=200, height=250, 
                  highlightbackground="yellow", 
                  highlightthickness=3, 
                  borderwidth=2, 
                  relief=RAISED)
middleFrame.pack()

bottomFrame=Frame(root)
bottomFrame.pack()

ourdirectory=[]

def load1():
    f1 = askopenfilename(filetypes=(('TXT files','*.txt'), ('All files', '*.*')))
    ourdirectory.append(f1)

mylab = Label(topFrame, text="Hello and welcome!")
mylab.pack()

button = Button(bottomFrame, text="Add File", command=load1)
button.grid(row=5, sticky=W)

root.mainloop()

When the "Add File" button is pressed, I expected the program to launch a popup window where the user can navigate in the file directory.当按下“添加文件”按钮时,我希望程序启动一个弹出窗口,用户可以在其中导航文件目录。 Instead, the title text just fades and it doesn't respond--and clicking on the window causes it to announce that it's stopped responding.取而代之的是,标题文本只是淡出而没有响应——点击窗口会导致它宣布它已停止响应。 (This also consistently causes the Jupyter kernel to die.) Still, when I run the code but close the main window without pressing the "File Add" button, it closes fine and nothing freezes or breaks. (这也始终导致 Jupyter 内核死亡。)不过,当我运行代码但关闭主窗口时没有按“文件添加”按钮,它关闭得很好,没有任何冻结或中断。

I have encountered the same issue when running some code that would open, modify, and overwrite an image.我在运行一些可以打开、修改和覆盖图像的代码时遇到了同样的问题。 Everything worked as expected until suddenly askopenfilename() started Not Responding and not opening the file dialog window.一切都按预期进行,直到突然 askopenfilename() 开始 Not Responding 并且没有打开文件对话框窗口。

I have attempted changing the initial directory of askopenfilename() and calling the method from a separate file and the command line without success.我尝试更改 askopenfilename() 的初始目录并从单独的文件和命令行调用该方法,但没有成功。

One of my open windows was a directory (not the same as I was working with) on which file thumbnails failed to load and files could not be opened.我打开的一个窗口是一个目录(与我使用的目录不同),无法加载文件缩略图并且无法打开文件。 Upon closing the window, the screen 'reloaded,' certain programs opened (such as sticky notes, which was previously closed but which typically opens upon restarting), and the method resumed normal functionality.关闭窗口后,屏幕“重新加载”,某些程序打开(例如粘滞便笺,以前关闭但通常在重新启动时打开),并且该方法恢复正常功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM