

System.Windows.Forms でFormを作成。  
以下を実行すると dialog.ShowDialog() でフリーズする。  
MessageBox は問題なく表示されます。  
Windows10、python 3.4.4 pythonnet 2.3.0 です。

* * *
[code]

    import clr
    clr.AddReference("System.Windows.Forms")
    clr.AddReference("System.Drawing")
    
    from System.Windows.Forms import Application, Form, TextBox
    from System.Windows.Forms import ToolBar, ToolBarButton, OpenFileDialog, MessageBox
    
    from System.Windows.Forms import DialogResult, ScrollBars, DockStyle, IWin32Window
    
    
    class IForm(Form):
    
        def __init__(self):
            self.Text = "OpenDialog"
    
            toolbar = ToolBar()
            toolbar.Parent = self
            openb = ToolBarButton()
    
    
            self.textbox = TextBox()
            self.textbox.Parent = self
            self.textbox.Multiline = True
            self.textbox.ScrollBars = ScrollBars.Both
            self.textbox.WordWrap = False
            self.textbox.Parent = self
            self.textbox.Dock = DockStyle.Fill
    
    
            toolbar.Buttons.Add(openb)
            toolbar.ButtonClick += self.OnClicked
    
    
            self.CenterToScreen()
    
        #@staticmethod
        def OnClicked(self, sender, event=None):
            MessageBox.Show("Test")
            dialog = OpenFileDialog()
            dialog.Filter = "C# files (*.cs)|*.cs"
    
            if dialog.ShowDialog() == DialogResult.OK:
                #dialog.ShowDialog()
                f = open(dialog.FileName)
                data = f.read()
                f.Close()
                self.textbox.Text = data
    
    
    Application.Run(IForm())
    
[/code]


CC BY-SA 3.0
2017-04-19T06:04:49.437
34117
2018-05-20T09:49:31.953
2017-04-19T06:12:18.203
5044
22640
question
python
Python clr OpenFileDialog でフリーズ


イベントから別のスレッドを呼び出すと動作しました。  
pythonから.Netフレームワークを呼び出すとき、Windowsのメッセージキューの処理やイベントとUIスレッドの関係などの知識を私は持っていないので、なぜ動くのかは補足できないのですが…。

[code]

    import clr
    clr.AddReference("System.Windows.Forms")
    clr.AddReference("System.Drawing")
    
    from System.Windows.Forms import Application, Form, TextBox
    from System.Windows.Forms import ToolBar, ToolBarButton, OpenFileDialog, MessageBox
    
    from System.Windows.Forms import DialogResult, ScrollBars, DockStyle, IWin32Window
    import threading
    
    class IForm(Form):
    
        def __init__(self):
            self.Text = "OpenDialog"
    
            toolbar = ToolBar()
            toolbar.Parent = self
            openb = ToolBarButton("OpenDialog")
    
            self.textbox = TextBox()
            self.textbox.Parent = self
            self.textbox.Multiline = True
            self.textbox.ScrollBars = ScrollBars.Both
            self.textbox.WordWrap = True 
            self.textbox.Parent = self
            self.textbox.Dock = DockStyle.Fill
    
            toolbar.Buttons.Add(openb)
            toolbar.ButtonClick += self.OnClicked
    
            self.CenterToScreen()
    
        #@staticmethod
        def OnClicked(self, sender, event=None):
            #スレッド化
            t = threading.Thread(target=self.selectFile)
            t.start()
    
        def selectFile(self):
            dialog = OpenFileDialog()
            dialog.Filter = "C# files (*.cs)|*.cs"
    
            if dialog.ShowDialog(self) == DialogResult.OK:
                #dialog.ShowDialog()
                f = open(dialog.FileName)
                data = f.read()
                f.close()
                self.textbox.Text = data
    
    Application.Run(IForm())
    
[/code]


CC BY-SA 3.0
2017-04-19T16:13:55.320
34127
2017-04-19T16:13:55.320
9820
34117
answer