[PyWin32] "Hello, world"?

G

Gilles Ganault

Hello

I'm reading O'Reily's "Python Programming on Win32", but couldn't find
a simple example on how to create a window with just a label and
pushbutton.

If someone has such a basic example handy, I'm interested.

Thank you.
 
T

Tim Golden

Gilles said:
Hello

I'm reading O'Reily's "Python Programming on Win32", but couldn't find
a simple example on how to create a window with just a label and
pushbutton.

Well, you might mean this:

<code>
import win32api

win32api.MessageBox (None, "Hello, World!", "Greetings")

</code>

but that obviously has limited possibilities for growth.
You can change the messages; you can play with combinations
of buttons; but that's about it.

Although I can provide a cut-down example, I think that these days
it would be quite rare to produce a raw-Windows GUI: you'd either
be using a .NET technology like the WPF or leveraging (as they
say in other parts of the world) an existing toolkit such as
Qt, wx or <insert your presentation toolkit of choice here>.

I suppose what I'm saying is that knowing the way in which
raw Windows GUI works is becoming increasingly irrelevant.
But I'm happy to produce something if you're still keen.

TJG
 
F

Francesco Bochicchio

Hello

I'm reading O'Reily's "Python Programming on Win32", but couldn't find
a simple example on how to create a window with just a label and
pushbutton.

This is probably because maybe the book addresses how to use python to
do windows-specific
stuff (like using a COM interface) and presumes a basic knowledge of
python in the reader
(just guessing, never read the book )

If someone has such a basic example handy, I'm interested.

Thank you.


There are as many way to do it as many GUI toolkits for python you can
find (and there are
many) although they all share a similar structure. Here is the one for
Tkinter, which is the
default python GUI toolkit. The example is copied verbatim from
the python on-line documentation ( section "Graphical User Interfaces
with Tk" of "The Python Standard Library").



Ciao
------
FB

from Tkinter import *

class Application(Frame):
def say_hi(self):
print "hi there, everyone!"

def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit

self.QUIT.pack({"side": "left"})

self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi

self.hi_there.pack({"side": "left"})

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
 
E

eb303

I'm reading O'Reily's "Python Programming on Win32", but couldn't find
a simple example on how to create a window with just a label and
pushbutton.

This is probably because maybe the book addresses how to use python to
do windows-specific
stuff (like using a COM interface) and presumes a basic knowledge of
python in the reader
(just guessing, never read the book )
If someone has such a basic example handy, I'm interested.
Thank you.

There are as many way to do it as many GUI toolkits for python you can
find (and there are
many) although they all share a similar structure. Here is the one for
Tkinter, which is the
default python GUI toolkit. The example is copied verbatim from
the python on-line documentation ( section "Graphical User Interfaces
with Tk" of "The Python Standard Library").

Ciao
------
FB

from Tkinter import *

class Application(Frame):
def say_hi(self):
print "hi there, everyone!"

def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit

self.QUIT.pack({"side": "left"})

self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi

self.hi_there.pack({"side": "left"})

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

Or way simpler:
---
from Tkinter import *
root = Tk()
Label(root, text='Hello world!').pack(side=TOP)
Button(root, text='Quit', command=root.quit).pack(side=BOTTOM)
root.mainloop()
 
C

CM

Hello

I'm reading O'Reily's "Python Programming on Win32", but couldn't find
a simple example on how to create a window with just a label and
pushbutton.

If someone has such a basic example handy, I'm interested.

Thank you.

In wxPython (which you would have to download and install), it could
be something like:

import wx

class Dialog1(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, id=-1, name='', parent=parent,
pos=wx.Point(132, 174), size=wx.Size(149, 80),
style=wx.DEFAULT_DIALOG_STYLE, title='')

self.staticText1 = wx.StaticText(id=-1,
label=u'Hello, World', name='', parent=self,
pos=wx.Point(40, 16), size=wx.Size(59, 13), style=0)

if __name__ == '__main__':
app = wx.PySimpleApp()
dlg = Dialog1(None)
try:
dlg.ShowModal()
finally:
dlg.Destroy()
app.MainLoop()
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top