PDA

View Full Version : A little python help, not that hard a question probably



kn100
July 20th, 2009, 11:39 PM
So, I have written a python program, where at the end of it there are about 15 variables, a,b,c,d,e and so on, and i am trying to make a GUI in wxGlade, but how do i get a textbox, or a statictext to display a variable, (the code that makes the variables is in the same file, and variables are defined before the GUI runs)

Thanks in advance
Kevin

Paul Miller
July 21st, 2009, 08:50 AM
It sounds like you should look at a basic wxPython tutorial, like the one at http://www.wxpython.org/tutorial.php .

ahmatti
July 21st, 2009, 10:11 AM
This is also a good tutorial: http://zetcode.com/wxpython/

Might be easier just to make the GUI with code examples in the tutorial than using wxGlade though.

kn100
July 21st, 2009, 01:19 PM
first one i've been through, second one i havent, so thanks!

kn100
July 21st, 2009, 01:32 PM
OK neither of those really helped, all i want to know is how in wxGlade to display a variable in a text box.

ahmatti
July 21st, 2009, 02:14 PM
I think wxGlade only generates the code for GUI and you need to add the event handlers to the code manually, thats why I posted the tutorial... Too bad it didn'h help!

kn100
July 21st, 2009, 05:17 PM
What is an event handler? I still dont understand that, i come from a Visual basic world, so take pity of a newbie :)

stevescripts
July 21st, 2009, 06:58 PM
google is your friend ...

http://en.wikipedia.org/wiki/Event_handler

http://www.webopedia.com/TERM/E/event_handler.html

Steve
(make sure your read about callbacks and event driven programming)

ahmatti
July 21st, 2009, 07:00 PM
Just take a look at the events section of the tutorial I posted, it explains way better than I can!

smartbei
July 22nd, 2009, 10:04 PM
Had a bit of a look at it...
Anyway, here is what you need to do:
A StaticText cannot be changed, so use a TextCtrl instead. Then, you can simply call its SetValue method with the string you want it to hold, and that's it. For an example, see the below code, which has a button that increments a counter, and updates a label with the current value:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Wed Jul 22 23:50:21 2009

import wx

# begin wxGlade: extracode
# end wxGlade



class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.label_1 = wx.TextCtrl(self, 2, "0")
self.button_1 = wx.Button(self, 0, "button_1")

self.__set_properties()
self.__do_layout()
# end wxGlade

self.Bind(wx.EVT_BUTTON, self.increment_and_update, id=0)
self.num = 0

def increment_and_update(self, event):
self.num += 1
self.label_1.SetValue(str(self.num))


def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
# end wxGlade

def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_2.Add(self.label_1, 0, wx.ADJUST_MINSIZE, 0)
sizer_2.Add(self.button_1, 0, wx.ADJUST_MINSIZE, 0)
sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
# end wxGlade

# end of class MyFrame

The Cog
July 22nd, 2009, 10:32 PM
I found this tutorial very useful:
http://www.micahcarrick.com/12-24-2007/gtk-glade-tutorial-part-1.html

And I attach two files - both with .txt.added to their names so the forum will let me upload them. The only bit of the program that actually works is the New menu, which resets the text content to "", but that should be enough to put you on the right track.