简体   繁体   English

Python tkinter askopenfilename() 没有打开和响应

[英]Python tkinter askopenfilename() not opening and responding

Python tkinter askopenfilename() is not opening and responding. Python tkinter askopenfilename() 没有打开和响应。 I searched and tried everything but it didn't work.我搜索并尝试了一切,但没有奏效。

What's wrong?怎么了?

Code:代码:

from tkinter.filedialog import askopenfilenames
import pygame, keyboard, time
import tkinter as tk
from tkinter.filedialog import askopenfilename

from pygame import key
pygame.init()
pygame.mixer.init()
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 450
BLACK = (0, 0, 0)
pyscreen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE)
def play(): 
    tkscreen = tk.Tk()
    tkscreen.iconbitmap("void.ico")
    tkscreen.title("Select music file...")
    print("b")
    global music
    tkscreen.update()
    music = askopenfilename() # Not responding!!
    tkscreen.update()
    print("c")
    try:
        pygame.mixer.music.load(music)
        print("d")
        pygame.mixer.music.play()
        print("e")
    except:
        print("e-1")
        print(f"{music} is not a music file. ")
print("a")
play()
pausecooldown = True
paused = False
while True:
    if keyboard.is_pressed("f9"):
        if keyboard.is_pressed("space") and pausecooldown:
            if paused:
                pygame.mixer.music.unpause()
                paused = False
                time.sleep(0.1)
                pausecooldown = False
            elif not paused:
                pygame.mixer.music.pause()
                paused = True
                time.sleep(0.1)
                pausecooldown = False
        pausecooldown = True
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exit()

Version: Python 3.9.1版本:Python 3.9.1

OS: Windows 10 Home操作系统:Windows 10

It prints 'b', and opens tkinter and pygame window, but 'Select file' window is not opening, and tkinter and pygame window is not responding. It prints 'b', and opens tkinter and pygame window, but 'Select file' window is not opening, and tkinter and pygame window is not responding.

I found an answer:我找到了答案:

Every tkinter instance requires a mainloop() method to be run.每个 tkinter 实例都需要运行mainloop()方法。 This method essentially monitors what's happening with the app and reacts to user inputs.这种方法本质上是监控应用程序发生的事情并对用户输入做出反应。 Without it, Tkinter just doesn't work.没有它,Tkinter 就无法工作。 You can add tkscreen.mainloop() just before the first try block and it should hopefully solve issues.您可以在第一个 try 块之前添加tkscreen.mainloop() ,它应该有望解决问题。

from above comment (by pavel )从上面的评论(帕维尔

I know and can understand your problem.我知道并且可以理解您的问题。

I was doing a Tkinter project once.我曾经做过一个 Tkinter 项目。 Well there is no better solution to this.好吧,没有更好的解决方案。 BUT I can help you with this anyways.但无论如何我都可以帮助你。


I have two solutions which I would like to share it with you.我有两个解决方案,我想与您分享。

1. Using Debugger I used this solution for the program when I was just editing my program. 1. 使用 Debugger当我刚刚编辑我的程序时,我对程序使用了这个解决方案。 If you use an IDE like VS Code or Pycharm.如果您使用 IDE,例如 VS Code 或 Pycharm。 Run the code in Debugger mode.在调试器模式下运行代码。 The program would work and respond.该程序将起作用并做出响应。

It will work as wanted.它会按需要工作。

2. Using.exe file of the project Use this link to understand how to do that. 2. 使用项目的.exe 文件使用此链接了解如何执行此操作。 It will just make your python program run as an application and as per me when I did this, the problem you mentioned didn't come up.它只会使您的 python 程序作为应用程序运行,并且按照我的说法,当我这样做时,您提到的问题没有出现。

https://datatofish.com/executable-pyinstaller/ https://datatofish.com/executable-pyinstaller/


If you still get any problem, please comment below and ask anything you want.如果您仍然遇到任何问题,请在下面发表评论并提出您想要的任何问题。

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

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