[英]Python tkinter askopenfilename() not opening and responding
Python tkinter askopenfilename() 沒有打開和響應。 我搜索並嘗試了一切,但沒有奏效。
怎么了?
代碼:
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()
版本:Python 3.9.1
操作系統: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.
我找到了答案:
每個 tkinter 實例都需要運行mainloop()
方法。 這種方法本質上是監控應用程序發生的事情並對用戶輸入做出反應。 沒有它,Tkinter 就無法工作。 您可以在第一個 try 塊之前添加tkscreen.mainloop()
,它應該有望解決問題。
從上面的評論(帕維爾)
我知道並且可以理解您的問題。
我曾經做過一個 Tkinter 項目。 好吧,沒有更好的解決方案。 但無論如何我都可以幫助你。
我有兩個解決方案,我想與您分享。
1. 使用 Debugger當我剛剛編輯我的程序時,我對程序使用了這個解決方案。 如果您使用 IDE,例如 VS Code 或 Pycharm。 在調試器模式下運行代碼。 該程序將起作用並做出響應。
它會按需要工作。
2. 使用項目的.exe 文件使用此鏈接了解如何執行此操作。 它只會使您的 python 程序作為應用程序運行,並且按照我的說法,當我這樣做時,您提到的問題沒有出現。
https://datatofish.com/executable-pyinstaller/
如果您仍然遇到任何問題,請在下面發表評論並提出您想要的任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.