Having trouble understanding using wxPython....:?:

import wx
class MainFrame(wx.Frame):
	def __init__(self):
		wx.Frame.__init__(self,None,wx.ID_ANY, title='Hello wolrd')
		self.buthello = wx.Button(self, wx.ID_ANY, label ='helo')
		self.buthello.Bind(wx.EVT_LEFT_DOWN, self.helloevent)
		self.Show()
	def helloevent(self, event):
		msg = 'hello'
		msgbox = wx.MessageDialog (self, message = msg, style = wx.OK)
		if msgbox.ShowModal() == wx.ID_OK:
			msgbox.Destroy()
	
app = wx.App(redirect = False )
frame = MainFrame()
app.MainLoop()

Q1. in the line 'wx.Frame.__init__', what is wx.ID_ANY, what is the purpose of it?.
Q2. Also self.buthello isnt defined anywhere? (confused here)
Q3. Also...

def helloevent(self, event):

how does 'event' work in here. Shouldnt it work without 'event'?
Q4. How can ShowModal be used in other cases?

import wx

app = wx.App()

frame = wx.Frame(None, -1, 'simple.py')
frame.Show()

app.MainLoop()

Q5. This is another tutorial i was looking at and on this it has used '-1' instead of wx.ID_ANY. whys that?

import wx

class Dockable(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        menubar = wx.MenuBar(wx.MB_DOCKABLE)
        file = wx.Menu()
        edit = wx.Menu()
        view = wx.Menu()
        insr = wx.Menu()
        form = wx.Menu()
        tool = wx.Menu()
        help = wx.Menu()

        menubar.Append(file, '&File')
        menubar.Append(edit, '&Edit')
        menubar.Append(view, '&View')
        menubar.Append(insr, '&Insert')
        menubar.Append(form, '&Format')
        menubar.Append(tool, '&Tools')
        menubar.Append(help, '&Help')
        self.SetMenuBar(menubar)

        self.Centre()
        self.Show(True)

app = wx.App()
Dockable(None, -1, 'Dockable menubar')
app.MainLoop()

Q6....how does lines 7-23 work. Also the use of '&'..??

Any tips and tricks will be much appreciated:D:X

1. wx.ID_ANY is a constant inside the wx module. It is used to pass into constructors that require an id when you don't want the resulting widget to have any specific id (you're telling it to give the widget any id that is available). Passing -1 in its place does the same thing.

2. Yes it is: self.buthello = wx.Button(self, wx.ID_ANY, label ='helo') self.buthello is defined here as a button with the label 'helo' (maybe you meant 'hello'?).

3.No. With self.buthello.Bind(wx.EVT_LEFT_DOWN, self.helloevent) you 'bind' self.buthello's left mouse button down event to the method self.helloevent, making self.helloevent an event handler. This means that wx will call self.helloevent whenever you hold the left mouse button down on the button, and it will call self.helloevent with an event object as the argument. Tip: use wx.EVT_BUTTON for button clicks.

6. Why dont you remove the '&' and see what happens? That's how you learn. As the lines 7 - 23, a menubar is created in line 7 and assigned to the variable menubar. Lines 8 - 14 create the different menus that would appear on the menu bar. Lines 16 - 22 assigns each menu to the menubar, giving each one a label. Line 23 just attaches the menubar to the frame.

self.buthello = wx.Button(self, wx.ID_ANY, label ='helo')
		self.buthello.Bind(wx.EVT_LEFT_DOWN, self.helloevent)

Q1. in the line 'wx.Frame.__init__', what is wx.ID_ANY, what is the purpose of it?.
Q5. This is another tutorial i was looking at and on this it has used '-1' instead of wx.ID_ANY. whys that?
Q2. Also self.buthello isnt defined anywhere? (confused here)

Q1 & Q5: id=wx.ID_ANY and id=-1 are both equivalent in saying "I don't really care what ID this object has"
Q2: self.buthello is defined above as a wx.Button

Q3. Also...

def helloevent(self, event):

how does 'event' work in here. Shouldnt it work without 'event'?

When a function is bound to an event (a key press or button click), the event parameter is required as it passes useful information such as which key was pressed, or what position the mouse was in at the time of event, etc.

In order to call one of these methods in your code (ie, without the event being triggered first) you'll have to define it as def helloevent(self, event=None): unless you want to have to pass it some garbage variable every time you call it.

Q4. How can ShowModal be used in other cases?

Not quite sure what you're asking here, but showModal() causes the window to come to the user's attention and require action before going away. This is particularly useful for dialog boxes, especially file select dialogs and message windows.

import wx

class Dockable(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        menubar = wx.MenuBar(wx.MB_DOCKABLE)
        file = wx.Menu()
        edit = wx.Menu()
        view = wx.Menu()
        insr = wx.Menu()
        form = wx.Menu()
        tool = wx.Menu()
        help = wx.Menu()

        menubar.Append(file, '&File')
        menubar.Append(edit, '&Edit')
        menubar.Append(view, '&View')
        menubar.Append(insr, '&Insert')
        menubar.Append(form, '&Format')
        menubar.Append(tool, '&Tools')
        menubar.Append(help, '&Help')
        self.SetMenuBar(menubar)

        self.Centre()
        self.Show(True)

app = wx.App()
Dockable(None, -1, 'Dockable menubar')
app.MainLoop()

Q6....how does lines 7-23 work. Also the use of '&'..??

First, menubar is defined as a wx.MenuBar, which is the thing at the top of your browser with File, Edit, View, History, etc...

Next, we declare each of the menu items as a Menu object. Typically each of these menu items would get items appended and bound by function. For example, on the file Menu object you would bind things like Open, New Tab, Print, etc. I don't know why this example does not do this.

Finally, we append each menu to the MenuBar via Append(object, display_name) and then set the window (self) to use the menubar that we've created.

The & (Ampersand) in the display_name of menu objects is used to denote the "Underlined" letter of a menu item. In your browser, if you hit Alt + 'F', you'll open the File menu because the F is "underlined". So in the above example, '&File' says that when the user presses Alt + 'F', they'll open the menubar item called 'File'... similarly if you defined it as 'Fil&e', the 'e' would get the underline and Alt + 'E' would open the menu item. Keep in mind this applies to menu sub-items as well, so hitting Alt + F + T (in Firefox) would open a "&File" -> "New &Tab".

Here's an example of adding menu items and binding them to methods (keep in mind this part of your code can get extremely bloated and large... best to keep in it's own function or in a different file entirely and return the menubar item to your main function):

# Build the menu bar
MenuBar = wx.MenuBar()
self.FileMenu = wx.Menu()
self.OptionsMenu = wx.Menu()
HelpMenu = wx.Menu()

# Build up File Menu
item = self.FileMenu.Append(-1, "&Synchronize", " Synchronize PC database with server")
self.Bind(wx.EVT_MENU, self.OnFile_Sync, item)
self.FileMenu.Enable(self.OnFile_Sync_Id, 1)

item = self.FileMenu.Append(-1, "Upload from &File", " Uploads logs from a Zip File to DataBase")
self.Bind(wx.EVT_MENU, self.UploadfromFile, item)

item = self.FileMenu.Append(-1, "&Copy to Thumb Drive", "Copy VCU image to thumb drive")
self.Bind(wx.EVT_MENU, self.OnFile_Copy2Thumb, item)
self.FileMenu.Enable(self.OnFile_Copy2Thumb_Id, 0)

item = self.FileMenu.Append(wx.ID_EXIT, "E&xit", " Exit this Tool")
self.Bind(wx.EVT_MENU, self.OnExit, item)

MenuBar.Append(self.FileMenu, "&File")

# Build up Options Menu
item = self.OptionsMenu.Append(-1, "VCU &IP Address", " IP address of the unit to connect with")
self.Bind(wx.EVT_MENU, self.OnOptions_IPAddr, item)
self.OptionsMenu.Enable(self.OnOptions_IPAddr_Id, 1)

item = self.OptionsMenu.AppendCheckItem(-1, "&Auto Delete Logs", " Delete source logs after download")
self.Bind(wx.EVT_MENU, self.OnOptions_Delete, item)
self.OptionsMenu.Check(self.OnOptions_Delete_Id, int(self.ini_del_after_get))

self.OptionsMenu.AppendSeparator()

item = self.OptionsMenu.Append(-1, "&Change Repository Location", "Changes repository location for synchronization")
self.Bind(wx.EVT_MENU, self.OnOptions_SetRepoLoc, item)

item = self.OptionsMenu.Append(-1, "&Set Database Options", "Sets database connection preferences")
self.Bind(wx.EVT_MENU, self.OnOptions_DBOptions, item)

MenuBar.Append(self.OptionsMenu, "&Options")

# Build up Help Menu
item = HelpMenu.Append(wx.ID_HELP, "&Help\tCtrl+H", "Opens help information")
self.Bind(wx.EVT_MENU, self.OnHelp, item)
item = HelpMenu.Append(wx.ID_ABOUT, "&About", " Information about this Tool")
self.Bind(wx.EVT_MENU, self.OnAbout, item)

MenuBar.Append(HelpMenu, "&Help")

self.SetMenuBar(MenuBar)

Using the Enable() method allows you to disable/enable menu items (disabled ones are grayed out and do nothing when clicked on). There are a lot of new things to understand in this method so please, ask what you don't understand and I'll explain...

import wx
class MainFrame(wx.Frame):
	def __init__(self):
		#wx.ID_ANY means wxpython should assign any unique identity you can write wx.ID_ANY or use -1
		wx.Frame.__init__(self,None,wx.ID_ANY, title='Hello wolrd')
		#here we define self.buthello as wxButton
		self.buthello = wx.Button(self, wx.ID_ANY, label ='helo')
		#here we bind the button to Left click event. Normal button event is wx.EVT_BUTTON
		self.buthello.Bind(wx.EVT_LEFT_DOWN, self.helloevent)
		#show self, which is now wxFrame
		self.Show()
	#You bound left click with this helloevent remember? Here is where you define it(try to remove it and see what happens)
	def helloevent(self, event):
		msg = 'hello'
		msgbox = wx.MessageDialog (self, message = msg, style = wx.OK)
		if msgbox.ShowModal() == wx.ID_OK:
			msgbox.Destroy()
	
app = wx.App(redirect = False )
frame = MainFrame()
app.MainLoop()

"""
Q1. in the line 'wx.Frame.__init__', what is wx.ID_ANY, what is the purpose of it?.
--- Assign any available ID to the widget

Q2. Also self.buthello isnt defined anywhere? (confused here)
--- It is define, see my comments above

Q3. Also...
	def helloevent(self, event)
	how does 'event' work in here. Shouldnt it work without 'event'?
---You cannot bind widget to nothing! You MUST bind to some methods. Here you define a method bound to widget (Here Just a hello messagebox)
	
Q4. How can ShowModal be used in other cases?
--- ShowModal is Dialogs attributes, you can use it wherever dialog boxes are used  

Q5. This is another tutorial i was looking at and on this it has used '-1' instead of wx.ID_ANY. whys that?
--- See my comments, its different way of saying same thing (Like True and 1)


Q6....how does lines 7-23 work. Also the use of '&'..??
---- Don't know which lines you are talking about. & mean to show little line under a next letter i.e &File means F will be underline (Look file menu in your word editor)

"""

Widgets in wxPython need a unique integer ID number. When you use -1 then wxPython will pick a unique ID number for you. BTW, wx.ID_ANY is a constant set to -1 and a little more descriptive. You also may want to look at class basics before you use them, so you understand self and __init__() better. Simple wxPython programs can be written without using class.

Here is ZZ's simple image viewer:

import wx

app = wx.App()
frame = wx.Frame(None, -1, "Show an image file")
wx.StaticBitmap(frame, -1, wx.Bitmap('clouds.jpg'))
frame.Show()
app.MainLoop()

My advice, don't bite more off than you can chew. Start with simple code and build up to explore the concepts of wxPython. That's the way to learn. Otherwise GUI stuff will overwhelm you!

There is a book out on wxPython, but it is full of errors and pretty bad. A relatively good tutorial is from:
http://wiki.wxpython.org/index.cgi/AnotherTutorial

If you haven't done so, take a look at DaniWeb's wxPython thread at:
http://www.daniweb.com/forums/thread128350.html

Also, I noticed that your first example uses tabs for indentation, avoid tabs and use four spaces instead. The code is much easier to read, and you don't run into the disastrous mistake of mixing tabs and spaces.

that was of much help, thanks a lot. I'm going through all your examples, and figuring out a style:idea:..

and a just few more questions:)

#'wxPython'-Menutest1.py
import wx
class menu (wx.Frame):
	def __init__(self):
		wx.Frame.__init__(self, None, wx.ID_ANY, title = 'test menu.py', size = (500,500))
		menubar = wx.MenuBar()
		'''---------------------'''
		file = wx.Menu()
		file.Append(wx.ID_ANY,'&Open','Open files')
		
		self.Bind(wx.EVT_LEFT_DOWN, self.OnOpen)
		'''---------------------'''
		edit = wx.Menu()
		edit.Append(wx.ID_ANY,'&Copy','Copy txt')
		edit.Append(wx.ID_ANY,'&Paste','Paste test')
		
		'''---------------------'''

		menubar.Append (file, '&File')
		menubar.Append (edit, '&Edit')
		self.SetMenuBar(menubar)
		self.Show()
	def OnOpen(self, event):
		msg = 'Opening File...'
		msgBox = wx.MessageDialog (self, message = msg, style = wx.OK)
		if msgBox.ShowModal() == wx.ID_OK:
			msgBox.Destroy()
		
	
app = wx.App()
menu()
app.MainLoop()

how do i get OnOpen to display the message when 'Open' is pressed?.

Also what is the difference between

app = wx.App()

and

app = wx.App(redirect = False)

Thank you.

To get an event handler on a menu item, you can use an id with Bind.

fileMenu = wx.Menu()
fileMenu.Append(wx.ID_OPEN, "&Open", "Open a file")
self.Bind(wx.EVT_MENU, self.OnOpen, id=wx.ID_OPEN)

Alternatively, you can do this:

fileMenu = wx.Menu()
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file")
openMenuItem.Bind(wx.EVT_MENU, self.OnOpen)

As for the second question, use a print statement somewhere in your program after wx.App is initialized, once where redirect = False is used, and once where it isn't. Observe the difference.

It directs wxPython where to show errors/consoleIO.
This will go to the console:
app = wx.App(redirect = False)
is the same as
app = wx.App(False)
or
app = wx.App(0)

This will go to a special window:
app = wx.App(True)
or since this is the default
app = wx.App()

When I develop a wxPython program with an IDE editor that has its own output windwow to show test prints and error messages, I use
app = wx.App(0)
Once everything works correctly, I simply remove the zero.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.