[Tutor] PyWin32 - Library of functions to interact with windows?
alan.gauld at btinternet.com
Wed Oct 14 19:38:31 CEST 2009
"Katt" <the_only_katala at verizon.net> wrote
> I am currently using WinXP and python 2.6.2
> I just installed PyWin32 v214 for python 2.6 from the following link:
Well done! :-)
> Now I find myself scratching my head and saying to myself: "now what?"
Good question. Why did you download and install it?
Did you have a specific need or did it just seem like a good idea at the
If the latter then the best thing to do is just ignore it unttil you have a
Except for the Pythonwin editor which is far superior to IDLE IMHO.
Definitely have a play with that.
> I have looked a bit at the documentation. It definitely seems to believe
> that I am already a seasoned programmer.
Yes, pywin is a set of modules to let you access the Win32 API - the
raw Operating System calls that are used by C programmers writing
things like MS Word and IE etc.
The typical Windows Python user rarely needs access to that stuff.
The exceptions are if you are writing sys admin type tasks on Windows.
> Searching the web I have found a lot of general information or overly
> specific with no way to narrow my search to what I am looking for. I am
> not sure, but it seems very difficult just to search for a way to change
> the color of my text.
That would normally be done using the GUI tookit you are using - Tkinter,
wxPython etc - you don't need the Win32 API for that the toolkit does that
> code snippet of what I need. I am just looking for an interpreter to
> translate from computer scientist to beginner.
The best bet is to find a book on basic Windows programming.
Pwetzold's "Programming Windows" used to be the Bible for this
but I suspect it has now been superceded. There are probably online
tutorials to writing MFC applications that you could translate fairly
The other area that you can and should use pyWin32 is in interacting
with Windows apps via COM. Mark Hammond's book "Python Programming
on Win32" is the best source for that, but the docs that come with pyWin32
also cover it. BUt again they expect a certain amount of preknowledge.
> I am not saying that there is anything wrong with what I have found. I
> am only saying that my comprehension is not there yet to that level.
pyWin32 is a powerful tool but it is not an easy one to use.
> Looking at the included compiled documentation I think I found what I am
> looking for. Please let me know if I am incorrect.
> Searching under "change the color" revealed several topics. I singled
> out win32gui.SetTextColor. This brought me to the topic "win32api.RGB"
> shows the next line as "int = RGB(red,green,blue).
> Question 1: Did I locate the correct function that I need to use in
> order to change the color of text within a print ""?
print goes to stdout which on Windows is usually in a CMD shell window.
the Win32API does not directl;y control that output. What you found is the
way tochange the colour of a font inside a window - such as a data entry
field in a form that you created.
> Question 2: In order to use this would I just import it? (ie - from
> win32api import RGB)
No. You need to import the function as well as the values.
Normally you impotrt the module and access the rest via that:
or similar. Note that the first parameter is hdc which stahnds for
Hamndle to a DeviceContext.
A DeviceContext is the window you want to change the color of.
So you first need to find a way of obtaining the hdc value.
In your own app thats fairtly easy since you assign it to a variable
when you create the window/widget. But trying to find the hdc for
a cmd shell is very tricky, especially programatically at run time!
> Question 3: If the above 2 are correct is it just a matter of doing the
> print "The remaining number of apples is: "RGB(red),number_apples
No, thats no good, sorry.
If you want to change the colour of text in a command window I think
you are best investigating wconio or some of the other modules
mentioned earlier. But it may not be possible for cmd.exe.
The next step is to write a very simple GUI program with a single
Text widget on which you write your output. Then you can colour it
any way you like.
Learning how to build GUIs wioll take a bit of effort but a lot less
than trying to use the Win32API to modify a cmd window!
Take a look at the Event driven oprogramming topic in my
tutorial for an example of just that kind of GUI... You can use
EasyGUI to capture user input.
Author of the Learn to Program web site
More information about the Tutor