views:

395

answers:

2

This is a two part question. A dumb technical query and a broader query about my possibly faulty approach to learning to do some things in a language I'm new to.

I'm just playing around with a few Python GUI libraries (mostly wxPython and IronPython) for some work I'm thinking of doing on an open source app, just to improve my skills and so forth.

By day I am a pretty standard c# bod working with a pretty normal set of MS based tools and I am looking at Python to give me a new perspective. Thus using Ironpython Studio is probably cheating a bit (alright, a lot). This seems not to matter because however much it attempts to look like a Visual Studio project etc. There's one simple behaviour I'm probably being too dumb to implement.

I.E. How do I keep my forms in nice separate code files, like the c# monkey I have always been ,and yet invoke them from one another? I've tried importing the form to be invoked to the main form but I just can't seem to get the form to be recognized as anything other than an object. The new form appears to be a form object in its own code file, I am importing the clr. I am trying to invoke a form's 'Show' method. Is this not right?

I've tried a few (to my mind more unlikely) ways around this but the problem seems intractable. Is this something I'm just not seeing or would it in fact be more appropriate for me to change the way I think about my GUI scripting to fit round Python (in which case I may switch back to wxPython which seemed more approachable from a Pythonic point of view) rather than try to look at Python from the security of the Visual Studio shell?

+1  A: 

I'm not totally clear on the problem. You can certainly define a subclass of System.Windows.Forms.Form in IronPython in one module, and then import the form subclass in some other module:

# in form.py
from System.Windows.Forms import Form, Button, MessageBox
class TrivialForm(Form):
   def __init__(self):
       button = Button(Parent=self, Text='Click!')
       button.Click += self.show_message

   def show_message(self, sender, args):
       MessageBox.Show('Stop that!')

# in main.py
import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import Application
from form import TrivialForm
if __name__ == '__main__':
   f = TrivialForm()
   Application.Run(f)

Is it that IronPython Studio's form designer/code generator won't let you structure your code like that? (I've never used it.)

wilberforce
A: 

In the end it was even simpler.

I was trying to invoke the subform thus:

f = frmSubform()
f.Show()

But I actually needed to do it this way

f = frmSubform()
Form.Show(f)

Form.ShowDialog(f) worked also; in a Dialog format of course.

A simple enough error but until you know you, well, don't know.

I'm not 100% sure I understand at this stage why what works, works, but I'm sure I shall learn with experience.