Created Form in System.Windows.Forms.
The following will freeze in dialog.ShowDialog().
The MessageBox is displayed without any problems.
Windows 10, python 3.4.4 pythonnet 2.3.0.
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, IWin32 Window
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())
Invoking another thread from the event worked.
When calling the .Net framework from python, I don't know how to handle Windows message queues or how events relate to UI threads, so I can't supplement why it works...
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, IWin32 Window
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):
# Threading
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())
© 2024 OneMinuteCode. All rights reserved.