1

I developed an IronPython-wpf application with GUI using XAML. The user enter all the inputs in the GUI and I can save them. My problem is how I could pass those variables to my main python code after USER closes the GUI? To close the GUI, I have a button, which closes the GUI, If the USER click it.

What I did is as follows (I just copied some portion of the code):

import wpf
class MyApp(Window):
    def __init__(self):
        self.opencfgfile()
        self.ictemp = self.FindName('pipent')
        self.ictemp.Text = self.ictest
        self.button = self.FindName('button')
        self.button.Click += self.onClick

    def onClick(self, sender, event):
        self.Close()

    def opencfgfile(self):
        sstest = os.environ['test']
        infile = open (sstest + '\config\test_Config.cfg' , 'r')
        for line in infile:
            if line != "\n":
               line = line[:-1]
               fields = line.split('=')
               if fields[0] == 'Initial test ID ':
                   self.ictest = fields[1].lstrip()

    def getsetname(self):
        try:
            return self.ictemp
        except AttributeError:
            return          

if __name__ == "__main__":
    c = MyApp()
    Application().Run(c)
    iset = c.getsetname()

In my class, if I put a break point, self.ictest has a value of 'test' and self.ictemp has a value of {System.Windows.Controls.TextBox:test}, however if I put the break point in my main program for iset, I will get this Value: 'The name iset does not exist in the current context. I really appreciate if you can help me on this issue.

Hamid K
  • 983
  • 1
  • 18
  • 40
  • What do you mean with back end? Do you want to embedd/host it in another application? – BendEg Dec 15 '15 at 20:03
  • Sorry for the confusion, I think that I am not using the terms correctly, I meant the main python code. – Hamid K Dec 15 '15 at 21:20

1 Answers1

0

Your problem is, that the code iset = c.getsetname() will only be reached, after the main window is closed. The reason for this is, that Application().Run() contains your "application main loop", which will run until the main window is closed. So in your case, you should implement all interaction logic in MyApp, not in the application entry point. Everything under if __name__ == "__main__" should only be used for initializing some modules or similar things.

If you want to encapsulate some logic, put it in own modules and call it from MyApp. For example if you want to react on some button click, just do this:

# Short
def __init__(self, ...):
    # Init component
    self.yourButton.Click += self.OnButtonClick

def OnButtonClick(self, sender, args):
    MessageBox.Show("Clicked on some button!") # Import Message box before :)

Hope this helps.

EDIT

This is working pretty well:

import wpf

from System.Windows import Window, Application

from System.Windows.Controls import TextBox

class MyApp(Window):

    box = None

    def __init__(self):
        self.Width = 200
        self.Height = 200

        self.box = TextBox()
        self.box.Height = 24
        self.box.Width = 150    

        self.Content = self.box 

    def getBoxContent(self):
        return self.box.Text

if __name__ == "__main__":
    c = MyApp()
    Application().Run(c)    
    print (c.getBoxContent())

Info passing

If you want to pass info to and from the ui, just create some class which hold the information. This will be the best solution:

import wpf

from System.Windows import Window, Application

from System.Windows.Controls import TextBox

class Information:

    InformationOne = 1
    InformationTwo = "Hello World"

class MyApp(Window):

    box = None
    information = None

    def __init__(self, info):
        self.information = info

        self.Width = 200
        self.Height = 200

        self.box = TextBox()
        self.box.Height = 24
        self.box.Width = 150    

        # Init information
        self.box.Text = self.information.InformationTwo

        self.Content = self.box 

    # Information property
    @property
    def Information(self):
        self.information.InformationTwo = self.box.Text

        return self.information

if __name__ == "__main__":

    info = Information()

    c = MyApp(info)
    Application().Run(c)    

    # Get information
    print (c.Information.InformationTwo)
BendEg
  • 20,098
  • 17
  • 57
  • 131
  • That is exactly what I want to do, after the main window is closed then transfer the inputs from User to the main code. I have a Submit button "self.button.Click += self.onClick", which closes the main window " def onClick(self,s,e): self.Close()". Is there any way to achieve this goal, except encapsulate everythin in MyApp Class? – Hamid K Dec 16 '15 at 14:18
  • Ok, if the window is closed, you can process in the main code. What is your excat problem there? `iset` will be available in the context after the code line `iset = c.getsetname()` is executed. – BendEg Dec 16 '15 at 14:19
  • If it is a "have to" and I do not have any other option then I will. However, when I used Tkinter to create all my GUI's I was able to create a new instance of my MyApp class and assign the object to a local variable "c" and do whatever I want to my class attributes. Thanks for your help. – Hamid K Dec 16 '15 at 14:24
  • @HamidK i've added one more sample, is that what you are looking for? Sorry, i do not get exactly what you want... – BendEg Dec 16 '15 at 14:29
  • @HamidK Add one more sample, which should fit to your needs. – BendEg Dec 16 '15 at 14:43
  • First, I do not see what is teh difference in your code with mine except yoy add .Text to return of the class function. Second, Since I am using Visual Studio I cannot see the print output so when i assign the output to iset variable and add that to Watch list i get the same error: does not exist in the current context – Hamid K Dec 16 '15 at 14:47
  • @HamidK you should get the output in the `Output` window of visual studio as well – BendEg Dec 16 '15 at 15:15
  • I still cannot understand why you need to have a property decorator added to pass information although with that is not working. This is a Python class you should be able to access to class methods after you create an instance of your class. – Hamid K Dec 17 '15 at 14:55