Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't work with tkinter.filedialog.askdirectory #580

Closed
lin1987www opened this issue Jun 24, 2021 · 5 comments
Closed

Can't work with tkinter.filedialog.askdirectory #580

lin1987www opened this issue Jun 24, 2021 · 5 comments
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend

Comments

@lin1987www
Copy link

lin1987www commented Jun 24, 2021

  • bleak version: 0.12.0
  • Python version: 3.9.5
  • Operating System: Win10 2004 (OS Build 19041.1052)

Description

when using import bleak and tkinter.filedialog.askdirectory will make program stop respond

What I Did

import bleak  # Even I didn't use anything, just import bleak, still cause problem
import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
dest_path = filedialog.askdirectory()

If remove the line

import bleak

it will work just fine.

@dlech
Copy link
Collaborator

dlech commented Jun 24, 2021

Given that you are using Python 3.9, I assume you are using the WinRT backend. This could be caused by microsoft/xlang#690 since both Bleak and Tkinter need to initialize the thread to use Windows APIs.

@dlech dlech added the Backend: WinRT Issues or PRs relating to the WinRT backend label Jun 24, 2021
@dlech
Copy link
Collaborator

dlech commented Jun 24, 2021

Does this work?

import bleak
import tkinter as tk
from tkinter import filedialog
from winrt import _winrt

_winrt.uninit_apartment()

root = tk.Tk()
dest_path = filedialog.askdirectory()

@lin1987www
Copy link
Author

lin1987www commented Jun 25, 2021

Yes! It works. Thanks you very much!

import bleak
import tkinter as tk
from tkinter import filedialog
from winrt import _winrt

_winrt.uninit_apartment()  # This line make filedialog.askdirectory() work

root = tk.Tk()
dest_path = filedialog.askdirectory()

But I still confuse, without _winrt.uninit_apartment() these similar methods below still work.

filedialog.asksaveasfile()
filedialog.asksaveasfilename()
filedialog.askopenfile()
filedialog.askopenfiles()
filedialog.askopenfilename()

What make filedialog.askdirectory() so special? Or maybe this is tkinter's issue?

If you have any idea, I would like to try.

@dlech
Copy link
Collaborator

dlech commented Jun 25, 2021

import winrt calls RoInitialize with the default RO_INIT_MULTITHREADED. Most GUI toolkits on Windows need a single thread apartment instead. I think it is just by luck that the other dialogs worked without locking up.

@hbldh
Copy link
Owner

hbldh commented Jun 28, 2021

This shed a lot of light on problems using GUIs with Bleak. This should be addressed in #266.

Good work finding uninit_apartment! That is many steps closer to a root cause than I have found so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend
Projects
None yet
Development

No branches or pull requests

3 participants