WPF Dialogs
import clr clr.AddReference("PresentationFramework") clr.AddReference("PresentationCore"); clr.AddReference('System.Data') clr.AddReference('System.Windows.Forms') import System from System import * from System.Windows import * from System.Windows.Controls import * from System.Windows.Forms import ( FolderBrowserDialog, DialogResult ) from Microsoft.Win32 import ( OpenFileDialog, SaveFileDialog ) def WpfAlertDialog(message,title=None): if title: MessageBox.Show(message,title) else: MessageBox.Show(message) def WpfYesNoDialog(message,title,icon=System.Windows.MessageBoxImage.Information): rv = MessageBox.Show(message,title,MessageBoxButton.YesNo,icon) if rv == MessageBoxResult.Yes: return True else: return False def WpfYesNoCancelDialog(message,title,icon=System.Windows.MessageBoxImage.Information): rv = MessageBox.Show(message,title,MessageBoxButton.YesNoCancel,icon) if rv == MessageBoxResult.Yes: return True elif rv == MessageBoxResult.No: return False else: return None def WpfFileOpenDialog(initialFile=None,multiselect=False): dlg = OpenFileDialog() dlg.Multiselect = multiselect dlg.DefaultExt = ".txt"; # Default file extension dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; if initialFile: dlg.InitialDirectory = System.IO.Path.GetDirectoryName(initialFile) dlg.FileName = System.IO.Path.GetFileName(initialFile) #Directory.GetParent(initialFile) #Path.GetFileName(initialFile) if dlg.ShowDialog() == True: return dlg.FileNames def WpfFileSaveDialog(initialFile=None): dlg = SaveFileDialog() dlg.DefaultExt = ".txt"; # Default file extension dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; if initialFile: dlg.InitialDirectory = System.IO.Path.GetDirectoryName(initialFile) dlg.FileName = System.IO.Path.GetFileName(initialFile) if dlg.ShowDialog() == True: return dlg.FileNames[0] def WpfDirectoryOpenDialog(initialDirectory=None): dlg = FolderBrowserDialog() dlg.ShowNewFolderButton = True if initialDirectory: dlg.SelectedPath = initialDirectory #Path.GetFileName(initialFile) if dlg.ShowDialog() != DialogResult.Cancel: return dlg.SelectedPath if __name__ == "__main__": import win import vbox def make_button(title,handler): b = Button(); b.Content = title; b.Click += handler; return b def yesno_dialog(sender,event): print( WpfYesNoDialog("YesNo Demo", "YesNo") ) def yesnocancel_dialog(sender,event): print( WpfYesNoCancelDialog("YesNoCancel Demo", "YesNoCancel") ) def fileopen_dialog(sender,event): files = WpfFileOpenDialog() for f in files: print(f) def filesave_dialog(sender,event): print( WpfFileSaveDialog() ) def diropen_dialog(sender,event): print( WpfDirectoryOpenDialog() ) app = win.Win("Dialog Demo",320,240) vb = vbox.WpfVBox(); vb.add_item( make_button( "Alert", lambda s,e: WpfAlertDialog("Alert Demo", "Alert") ) ) vb.add_item( make_button( "YesNo", yesno_dialog ) ) vb.add_item( make_button( "YesNoCancel", yesnocancel_dialog ) ) vb.add_item( make_button( "FileOpen", fileopen_dialog ) ) vb.add_item( make_button( "FileSave", filesave_dialog ) ) vb.add_item( make_button( "DirOpen", diropen_dialog ) ) app.set_content(vb.ctrl) app.run()
댓글 없음:
댓글 쓰기