tags:

views:

580

answers:

11

I am an undergraduate student. I was exposed to basic programming couple of years back in school. Till now I have an understanding of Core Java, Core Python and basic C and C++.

Every time I start off with some GUI programming so as I can start off with a project of mine, I get boggled by the sheer amount which is to be done, API to be learnt, MVC architecture and everything programmers talk about, event handling etc etc.

Studied awt and swings for a while. Tried my hands on Qt and Gtk, could not find much of documentation. Tried to make sense of pygame. I end up at the same place, knowing the core language.

Tkinter on my zenwalk Linux is broken so could never start it athough I own a book on python with Tkinter explained.

But I end up at the same place, with just the basic understanding of the language.

Want to start over, seriously now. I would like to choose python. How should I go about studying GUI programming?

I need some Internet resources and direction so that I don't end up at the same place!

+2  A: 

If you are leaning more to games...

I suggest you install Pygame and Python, and go through their tutorials. The pick a simple game or graphics project and program it!

gahooa
+1  A: 

What do you mean by "Graphics"? Do you mean game graphics, or do you simply mean user interface code (forms, webpages, that sort of thing)? In the case of game graphics, there's a limit to how simple things can be made, but http://www.gamedev.net, for example, has tons of introductory articles on 2d and 3d engines. For something more along the application line, you might simply download Visual Studio or Eclipse and spend some time looking at the code that is autogenerated by their WYSIWYG editors.

Mike Burton
+5  A: 

Since it sounds like you want Python GUI programming, may I suggest PyGTK?

That's probably a pretty good place to start for someone who knows Python and would like to start small on some basic GUI apps. GTK can be complex at times, but with PyGTK there's plenty of open-source example apps you can study, from simple to complex.

Edit: This tutorial from LinuxJournal seems pretty helpful.

Edit 2: Here's the tutorial from PyGTK's site, and another tutorial I randomly found from Google (seems like that whole blog is pretty useful for what you want to do, actually). Finally, the snippet at the bottom of this page might be helpful, courtesy of Ubuntu's forums.

Reynolds
Thanks Reynolds. Your post is helpful. :)
Myth17
Can I have more resources and ebooks for pygtk??
Myth17
Tried to add a few more for you, let me know if those help.
Reynolds
A: 

If you have already gone through pygame, tk, Qt, and GTK, then really the only thing left that I can think of is pyglet, which I admit I have not tried, but I have read uniformly good things about it.

Still, more than anything it sounds as though you have trouble sticking with a framework long enough to really grok it. May I recommend starting with a small project, such as Pong or Breakout, and only learning as much as you need to make it? Once you have finished one thing, you will have a feel for the library, and continuing past there is a lot easier.

Nikwin
A: 

whatever language you choose you will have to deal with the many details involving GUI programing. this is due to the nature of the window based environment usually used for GUI.

what can help you move forward quickly in developing GUI based application is less the language and more the IDE you use. a good IDE can do some part of the less interesting stuff for you letting you focus on the big picture.

with C# in VS 2008 its all about choosing elements and methods from lists boxes. its very easy to get started and have a working project.
you can then try to customize your application to gain better understanding of whats going on behind the scenes

Alon
+2  A: 

For Python GUIs I like wxPython (www.wxpython.org). It is pretty easy to get started with simple controls and layouts. It is also cross platform. Plenty of tutorials out there. Just search for wxPython tutorial.

Josh
I haven't used it myself, but I have often heard good things about wxPython. This might be a good alternative solution for your needs if you find PyGTK frustrating, although I can't give you any first-hand experiences.
Reynolds
wxPython is great! I haven't used it in a while, but the documentation used to be rather incomplete. It used to force you to go back to the wxWindows C++ API documentation, which is a problem if you don't know C++.
Chinmay Kanchi
A: 

One of the greatest Python GUI you can study from is the source of IDLE. It always comes with Python.

Noctis Skytower
A: 

For GUI work in general:

Less is more

GUI work (even in productive frameworks) is about as fun and productive as painting the Eiffel Tower with a toothbrush. Go for a minimal design.

Avoid State Like The Plague

Do you put state in your GUI, or in the model? If you put it in the GUI, you are going to mess yourself up with redundant and inconsistent code paths. If you put it in the model, you risk an overly complex system that gets out of sync when your GUI fails to update from the model. Both suck.

wxPython

If you want to learn wxPython, here are a few traps I noticed:

The tutorial

Use this tutorial - http://wiki.wxpython.org/AnotherTutorial

It's the best one I found.

But remember to toggle line numbers, for easy pasting.

Events

Events are a bit like exceptions, and they are used to make things interactive.

In a vanilla python program, you write something like:

 def doit(i): 
      print 'Doing i = ',i

for i in range(10):
    doit()

print 'Results = ',result

In a GUI, you do something like:

 def doit(event): 
     print 'An event',event,'just happened!'
     event.Skip()

import wx
app = wx.App() 
frame = wx.Frame(None, -1, 'The title goes here') 
frame.Bind(wx.EVT_KEY_DOWN, doit)
frame.Show()
app.MainLoop()

Every time the user presses a key down, an event will be raised. Since frame is bound to the event (frame.Bind(wx.EVT_KEY_DOWN, doit)), the function doit will be called with the event as an argument.

Printing to stderr isn't too hot in a gui, but doit could also call up a dialog, or do anything you want it to.

Also, you can generate your own events using timers.

Apps, Frames, Windows, Panels, and Sizers

Everything has a parent. If an event is raised, and the child doesn't skip it (using event.Skip()), then the parent will also have to handle the event. This is analogous to exceptions raising up to higher-level functions.

A wx.App is like the Main function.

wx.Window isn't really used. Stuff inherits from it, and it has all the methods for sizing and layout, but you don't need to know that.

wx.Frame is a floating frame, like the main window in Firefox. You will have main one frame in a basic application. If you want to edit multiple files then you might have more. A wx.Frame won't usually have parents.

wx.Panel is part of a parent window. You can have several panels inside a frame. A panel can have a wx.Frame as a parent, or it might be the child of another panel.

wx.Sizers are used to automatically layout panels inside frames (or other panels).

Code:

def doit1(event):
    print 'event 1 happened'

def doit2(event): 
     print 'event 2 happened'

import wx
app = wx.App()
frame = wx.Frame(None, -1, 'The title goes here') 

panel_1 = wx.Panel(frame,-1,style=wx.SIMPLE_BORDER) 
panel_2 = wx.Panel(frame,-1,style=wx.SIMPLE_BORDER)

panel_1.Bind(wx.EVT_KEY_DOWN, doit1)
panel_2.Bind(wx.EVT_KEY_DOWN, doit2)

panel_1.SetBackgroundColour(wx.BLACK)
panel_2.SetBackgroundColour(wx.RED)

box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(panel_1,1,wx.EXPAND)
box.Add(panel_2,1,wx.EXPAND)

frame.SetSizer(box)

frame.Show()

app.MainLoop()

I've been really bad, and not used OOP practices. Just remember that even if you hate OO in most contexts, GUI programming is the place where OOP really shines.

The MCV

I don't get MCV. I don' think you need an MCV. I think a MW (model-widget) framework is fine.

For example - 2 frames that edit the same piece of text:

class Model(object):
    def __init__(self):
        self.value = 'Enter a value'
        self.listeners = []

    def Add_listener(self,listener):
        self.listeners.append(listener)

    def Set(self,new_value):
        self.value = new_value
        for listener in self.listeners:
            listener.Update(self.value)


import wx
app = wx.App() 

class CVFrame(wx.Frame):
    def __init__(self, parent, id, title, model):
        wx.Frame.__init__(self, parent, id, title, size = (100,100))
        self.button = wx.Button(self, -1, 'Set model value')
        self.textctrl = wx.TextCtrl(self, -1,model.value)
        self.button.Bind(wx.EVT_BUTTON,self.OnSet)

        self.model = model
        model.Add_listener(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.button,0,wx.EXPAND)
        sizer.Add(self.textctrl,1,wx.EXPAND)

        self.SetSize((300,100))
        self.SetSizer(sizer)
        self.Center()
        self.Show()

    def OnSet(self,event):
        self.model.Set(self.textctrl.GetValue())

    def Update(self,value):
        self.textctrl.SetValue(value)

model = Model()
frame1 = CVFrame(None, -1, 'Frame 1',model)
frame2 = CVFrame(None, -1, 'Frame 2',model) 
app.MainLoop()

wxPython has a listener-subscriber framework, which is a better version of the model I just sketched out (it uses weak refs, so deleted listeners don't hang around, and so on), but that should help you get the idea.

wisty
Shouldn't that be MVC, not MCV?
Joel
+1  A: 

I know how you feel--I learned a whole lot of computer programming during my CS degree but very little about GUIs. I ended up teaching myself Cocoa/Objective-C for a project. Cocoa is wonderful for GUI stuff but often a royal pain with a steep learning curve. If you don't have any experience with C programming, don't bother.

First step: familiarize yourself with the MVC (Model/View/Controller) design convention, because nearly every GUI framework will reference it. Google it--there are lots of resources about it. My quick, simple definition is:

The model level defines the data or the logical model for the application. For a web app, that would be the database. For a game, it could be stored data and game logic/rules.

The view level is what the user sees and interacts with (the GUI).

The controller level is the logic that connects the two. For example, the controller knows that when you click the "start game" button in the view level, it does some stuff with the model (say, setting up the board and the players.)

Step two: Figure out what you want. Are you interested in desktop applications specifically? Games? Web apps?

If mostly what you want to do is to be able to develop something that people would actually use, another option is to learn a web development framework. The frameworks make stuff easy for you. I love Django, personally, and if you know a little Python and a little HTML and a little about MVC, you can pick it up quickly. (Just don't be confused, because what Django calls a view is actually a controller.)

If what you want to do is games or graphics/animation stuff, check out pygame. I used it for a class project--basically taught it to myself in a couple of weeks--and it worked great.

I'd say stay as far away as you can from Java Swing/awt/etc.

I've heard good things about wxPython--I almost ended up using it instead of Cocoa, because the wx stuff is available in several programming languages and it's all cross platform.

Good luck! Stay strong! I know it's really intimidating, because I've been in your shoes. You can do it with some work, practice, and motivation.

Ellie P.
Thanks Ellie. :)
Myth17
Btw why stay away from Java Swing/awt/etc ??
Myth17
In my experience, developing GUI applications in Java is a huge pain. It's difficult to make a really good looking interface, it's hard to get things to do what you want, and you have to write a LOT of code just for basic GUI stuff. (Like an event listener for every event, etc.)
Ellie P.
Plus AWT is rather outdated now.
Reynolds
+1  A: 

Many have recommended wxPython, and I second their enthusiasm - it is a great framework; it also includes a serious demo (with code and live applications) which will be extremely valuable for learning.

Now, BEWARE!

It is very simple to confuse the end with the means. Programming GUIs can be extremely attractive but not very productive. In my early days I spent days and days trying to get a simple plotting application (reinventing the wheel); a simple GUI for solving quadratic equations; a simple GUI for calling database queries by clicking on certain locations on a map, etc. During all this time I never actually dug into algorithms or more general and productive computer science and computer engineering topics. In retrospect, I should have. Granted, I did learn a lot and I don't totally regret it, but my advice stands: worry about your algorithm first and about your interface second. This may not apply to every field (I am an engineer for NASA). Nowadays I work with number crunching applications with no GUIs whatsoever; I don't think they need them!

Anyway, I just wanted to share my two cents with GUI programming - have fun but don't overdo it.

Arrieta
The best thing about web interfaces is that their minimal design (due to the limitations of web programming). If Larry and Page had more than basic web design skills when they coded the google interface, it might have been a lot worse than it turned out to be.Less is more. It's also cheaper.
wisty
A: 

For Java, you could also look into SWT.

While I have never used AWT or Swing, I have read that SWT is the easiest of the three to learn.

Here is a decent comparison between the three.

juan2raid