Building basic dialog in Windows?

F

Fred

Hi,

I'd like to turn a command-line script into a Windows GUI app
using the native widgets so as to reduce the size of the binary (ie.
no QT, wxWidgets, et al.)

I came up with the following list of tools to access the Win32 API:

- PythonWin (MFC) http://www.python.org/windows/pythonwin/
- ctypes http://starship.python.net/crew/theller/ctypes/
- EasyDialogs for Windows
http://www.averdevelopment.com/python/EasyDialogs.html
- DynWin http://www.nightmare.com/~rushing/dynwin/
- sdk32 - Partial Python wrap of the Win32 Platform SDK
http://www.object-craft.com.au/projects/sdk32/

Does someone have a sample on how to display an OK/Cancel dialog with
a label + progress bar? Should I look at other tools?

Thank you
Fred.
 
T

Thomas Heller

Fred said:
Hi,

I'd like to turn a command-line script into a Windows GUI app
using the native widgets so as to reduce the size of the binary (ie.
no QT, wxWidgets, et al.)

I came up with the following list of tools to access the Win32 API:

- PythonWin (MFC) http://www.python.org/windows/pythonwin/
- ctypes http://starship.python.net/crew/theller/ctypes/
- EasyDialogs for Windows
http://www.averdevelopment.com/python/EasyDialogs.html
- DynWin http://www.nightmare.com/~rushing/dynwin/
- sdk32 - Partial Python wrap of the Win32 Platform SDK
http://www.object-craft.com.au/projects/sdk32/

You've missed venster http://venster.sf.net/
Does someone have a sample on how to display an OK/Cancel dialog with
a label + progress bar? Should I look at other tools?

I think pywin32 has what you need, the makepy utility displays such a
progress dialog when creating python wrappers for large typelibraries.

Thomas
 
F

Fred


Oops :) It's still alpha and documentation barely exists, so I guess
PythonWin/pywin32 looks like a better choice.
I think pywin32 has what you need, the makepy utility displays such a
progress dialog when creating python wrappers for large typelibraries.

OK, I'll check it out. It seems like pywin32 is meant for C++
developers who are already proficient with MFC, and the only
documentation is pretty much the scripts that comes with the software,
but I'll try to figure it out and extract what I need.

Thank you
Fred.
 
L

Larry Bates

Here's a class for Windows progress bar:

#
# Progress bar control example
#
# PyCProgressCtrl encapsulates the MFC CProgressCtrl class. To use it,
# you:
#
# - Create the control with win32ui.CreateProgressCtrl()
# - Create the control window with PyCProgressCtrl.CreateWindow()
# - Initialize the range if you want it to be other than (0, 100) using
# PyCProgressCtrl.SetRange()
# - Either:
# - Set the step size with PyCProgressCtrl.SetStep(), and
# - Increment using PyCProgressCtrl.StepIt()
# or:
# - Set the amount completed using PyCProgressCtrl.SetPos()
#
# Example and progress bar code courtesy of KDL Technologies, Ltd., Hong
Kong SAR, China.
#

from pywin.mfc import dialog
import win32ui
import win32con
import time

def MakeDlgTemplate():
style = (win32con.DS_MODALFRAME |
win32con.WS_POPUP |
win32con.WS_VISIBLE |
win32con.WS_CAPTION |
win32con.WS_SYSMENU |
win32con.DS_SETFONT)
cs = (win32con.WS_CHILD |
win32con.WS_VISIBLE)

w = 215
h = 36

dlg = [["Progress bar",
(0, 0, w, h),
style,
None,
(8, "MS Sans Serif")],
]
return dlg

class TestDialog(dialog.Dialog):
def OnInitDialog(self):
rc = dialog.Dialog.OnInitDialog(self)
self.pbar = win32ui.CreateProgressCtrl()
self.pbar.CreateWindow (win32con.WS_CHILD |
win32con.WS_VISIBLE,
(10, 10, 310, 24),
self, 1001)
return rc

def demo():
d = TestDialog (MakeDlgTemplate())
d.CreateWindow ()
for i in xrange(100):
d.pbar.SetPos(i)
time.sleep(0.1)

d.OnCancel()

if __name__=='__main__':
demo()


Here's some code for a file dialog box that I stripped from
an application that asks user to select an export file
from TimePilot payroll application (the files are always
called TimePilot.mdb):

import win32ui
import win32con

#---------------------------------------------------------------------------
----
# Display a file dialog box that allows the user to browse to the
TimePilot.mdb
# file that they wish to export.
#---------------------------------------------------------------------------
----
timepilotdatapath="C:\TimePilot"
f=win32ui.CreateFileDialog(1, None, "TimePilot.mdb", 0,
'TimePilot databases|TimePilot.mdb||')
f.SetOFNInitialDir(timepilotdatapath)
f.SetOFNTitle("Timepilot Extracted Data")
result=f.DoModal()
if result == win32con.IDCANCEL:
emsg="Cancel button pressed during TimePilot.mdb file selection,
aborting"
sys.exit(emsg)

mdb_path=f.GetPathName()

HTH,
Larry Bates
Syscon, Inc.
 
F

Fred

OK, I'll check it out. It seems like pywin32 is meant for C++
developers who are already proficient with MFC, and the only
documentation is pretty much the scripts that comes with the software,
but I'll try to figure it out and extract what I need.

After comparing the scripts under \Demo, here's how to display a basic
dialog in Windows with PythonWin/PyWin32, with a label, and two
pushbuttons, OK/Cancel. When you click on OK, the text of the label
changes, and the OK button is disabled:

from pywin.mfc import dialog
import win32con

dlgStatic = 130
dlgButton = 128

class Mydialog(dialog.Dialog):
def OnInitDialog(self):
rc = dialog.Dialog.OnInitDialog(self)
return rc

def OnOK(self):
label=self.GetDlgItem(dlgStatic)
label.SetWindowText("Text changed")
button=self.GetDlgItem(win32con.IDOK)
button.EnableWindow(0)

def OnCancel(self):
self.Cancel = 1
self._obj_.OnCancel()

style = (win32con.DS_MODALFRAME |
win32con.WS_POPUP |
win32con.WS_VISIBLE |
win32con.WS_CAPTION |
win32con.WS_SYSMENU |
win32con.DS_SETFONT)
cs = win32con.WS_CHILD | win32con.WS_VISIBLE
s = win32con.WS_TABSTOP | cs
w = 184
h = 40

dlg = [["PyWin32",(0, 0, w, h), style, None, (8, "MS Sans
Serif")],]

dlg.append([dlgStatic, "Click on OK", dlgStatic, (7, 5, 69, 9), cs |
win32con.SS_LEFT])
dlg.append([dlgButton, "OK", win32con.IDOK, (7, 20, 50, 14), s |
win32con.BS_DEFPUSHBUTTON])
s = win32con.BS_PUSHBUTTON | s
dlg.append([dlgButton, "Cancel", win32con.IDCANCEL, (124, 20, 50, 14),
s])

d = Mydialog(dlg)
d.DoModal()

My .15E/.20$
Fred.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top