|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
|
|
|
Re: PyWin32 - Library of functions to interact with windows?"Katt" <the_only_katala@...> 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 time? If the latter then the best thing to do is just ignore it unttil you have a need. 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 for you. > 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 directly. 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" > and > 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 ""? Probably not. 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: import win32gui win32gui.SetTextColor(hdc, win32gui.RGB) 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 > following: > 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. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: PyWin32 - Library of functions to interact with windows?If I may chime in...
As Alan said, pywin is basically a thin wrapper around the Win32 API. The Win32 API is very complex. Thus pywin is, by necessity, also very complex. There is documentation for pywin, but it is very minimal as you've probably noticed. If you are a *very* bold beginner with lots of patience, the best documentation for pywin is actually the documentation for the Win32 API itself found on Microsoft's "MSDN Library" website [1]. Navigating and using this documentation requires a knowledge of the Windows architecture and C programming. Once you find what you want in the MSDN documentation, you can usually find the same function in pywin. Again, this is not going to be easy if you are a beginner (it is difficult even for more experienced programmers), but I wanted to let you know the info is out there. If you want to dive into Win32 programming and learn the basics, the books Alan mentioned are good. But again, Win32 programming is pretty low-level and complex. There is usually an easier way to do most things you need to do. Especially if you want to create GUI's in Python, don't start with pywin and Win32. Use EasyGUI, Tkinter, or wxPython (in order of easiest to most powerful)
Now, back to your specific task. To clarify, I'm assuming you want to color the text that shows up in Window's console when you do "print" from Python, correct? It *is* possible to color console text with Python and pywin. But, it is tricky and not obvious. I've been wondering how to do this myself and I recently found some C code on the web [2] that does this and I translated that into to Python and pywin. It can be done in about 4 lines of Python.
To get you started, here is the link [3] to the MSDN documentation that tells you what you need to know about coloring text in a Windows console window (what Python's print command uses). Then, it is up to you to translate this into Python and pywin. The link [2] could also help. If you are up for a challenge, give it a shot. If you get stuck or it takes too long, write back and I/we can nudge you in the right direction with a code snippet.
(While writing this email, I also discovered that you can control the cursor position in a win32 console with pywin! Fun!)
Best of luck, and feel free to ask for more help!
-Scott
_______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: PyWin32 - Library of functions to interact with windows?On Wed, Oct 14, 2009 at 12:38 PM, Katt <the_only_katala@...> wrote:
> 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. Did you look at the console and wconio links Tim Golden provided? They both include methods to set the console text color. Looking at the source for wconio, I see it calls SecConsoleTextAttribute() to set the color. That function is documented here: http://msdn.microsoft.com/en-us/library/ms686047%28VS.85%29.aspx I can't help you figure out how to use this from Python using win32 though... Kent _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: PyWin32 - Library of functions to interact with windows?"Scott Nelson" <sirgnip@...> wrote > myself and I recently found some C code on the web [2] that does this and > I > translated that into to Python and pywin. It can be done in about 4 > lines > of Python. Yes, its much easier than I expected. I had never seen the SetConsoleTextAttribute API call before nor the GetStdHandle for accessing stdout. Thanks for the link. > (While writing this email, I also discovered that you can control the > cursor > position in a win32 console with pywin! Fun!) I'd still go for wconio for that kind of stuff - including changing colors... http://newcenturycomputers.net/projects/wconio.html Alan G _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: PyWin32 - Library of functions to interact with windows?Katt wrote:
> Hello all, > > I am currently using WinXP and python 2.6.2 > I just installed PyWin32 v214 for python 2.6 from the following link: [... snip lots of advice from other people ...] Hi, Katt. Thanks for posting back over here. I must admit, I hadn't realised you were posting to the tutor list when I answered your question originally. (I subscribe to several Python lists and they come into the same mailbox). I rather assumed you were an experienced programmer who just wanted this snippet of win32 info. I'd have been a bit more explanatory if I'd realised. It looks like you've got some useful advice from other people which will hopefully take you somewhere, but some more general advice: it's always a good idea to explain what you're trying to achieve ("I'm trying to write a hangman game which will run in a console window" or "I want to use a GUI to display the text from a web page" or whatever) rather than how you think you want to do it ("How to I position characters at locations on a Console?" or "How do convert HTML colour codes to Windows colour codes") It makes it easier for people trying to help to give the right context to their answers. Good luck and keep posting back here with questions if you have them TJG _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: PyWin32 - Library of functions to interact with windows?Scott Nelson <sirgnip@...> wrote:
For the sake of posterity (and the archives), I figured I'd list the "4 lines of Python" I mentioned above:
<code>
import win32console
handle = win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE) handle.SetConsoleTextAttribute(win32console.FOREGROUND_BLUE) print 'blue text' </code>
Beginning with this snippet, there are a number of other things you can do, such as set the cursor position (handle.SetConsoleCursorPosition) and get the properties of the console itself (handle.GetConsoleScreenBufferInfo).
_______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
| Free embeddable forum powered by Nabble | Forum Help |