python filedialog打开的对话框啥呀没有_Pythontkinter.filedialog文件askfolder干扰

本文介绍了在Python中使用filedialog时遇到的问题,对话框无法正常显示。问题根源在于COM线程模型,即MTA模式导致。解决方法包括使用Tkinter在STA线程中启动对话框,或者在调用CLR前强制设置STA模式。
摘要由CSDN通过智能技术生成

你的问题相当平庸,虽然不那么明显。问题不在tinker或pythonnet中,它源于COM线程模型。在

首先,由于您使用的是clr,所以让我们尝试直接使用对话框(不一定要导入tinker模块):# importing pythonnet

import clr

# adding reference (if necessary) to WinForms and importing dialogs

# clr.AddReference('System.Windows.Forms')

from System.Windows.Forms import OpenFileDialog, FolderBrowserDialog

# creating instances of dialogs

folder_dialog = FolderBrowserDialog()

file_dialog = OpenFileDialog()

# try to show any of them

folder_dialog.ShowDialog()

file_dialog.ShowDialog()

如你所见,它就像你的箱子一样挂着。如上所述,原因来自线程的单元状态([1],[2])。在

因此,clr隐式将此状态设置为MTA(多线程单元),可以通过^{}函数对其进行测试:

^{pr2}$

但是,许多较旧的COM对象(如shell对话框)需要STA模式。

关于这两种状态之间区别的很好的解释可以在here或there中找到。在

最后,解决方案:

1)对话框使用STA线程:# impo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值