LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2015, 09:18 AM   #1
Nexusfactor
Member
 
Registered: Jan 2015
Distribution: Ubuntu
Posts: 50

Rep: Reputation: Disabled
Python - Passing Data Between Forms


Form 1:

Code:
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"Enter text here.")

        button = Tkinter.Button(self,text=u"Click me !",
                                command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.labelVariable = Tkinter.StringVar()
        label = Tkinter.Label(self,textvariable=self.labelVariable,
                              anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW')
        self.labelVariable.set(u"Hello !")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())       
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" )
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()
Subform:

Code:
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter

class subform_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky='EW')

if __name__ == "__main__":
    app = subform_tk(None)
    app.title('Subform')
    app.mainloop()
How would I pass the string that was entered in the textbox of form 1 to the textbox of the subform when the button is clicked?

In C# you can use the constructor, in the subform to pass the data and then set the textbox that way, can I do the same in python?

Last edited by Nexusfactor; 05-31-2015 at 10:39 AM.
 
Old 06-01-2015, 04:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
Do you mean something like this?
Code:
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter

class subform_tk(Tkinter.Tk):
    def __init__(self,parent,data):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize(data)

    def initialize(self,data):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.insert(0,data)
        self.entry.grid(column=0,row=0,sticky='EW')

if __name__ == "__main__":
    app = subform_tk(None)
    app.title('Subform')
    app.mainloop()
and you only need to call:
subform.subform_tk(None,<text>)
 
Old 06-01-2015, 06:00 AM   #3
Nexusfactor
Member
 
Registered: Jan 2015
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
Thank You For This!! I think it's Exactly what I wanted.

Code:
private void button1_Click(object sender, System.EventArgs e) 
{

     FormTwo handleData= FormTwo(txtUserInput.Text)
     handleData.showDialog();

}

and in form 2 I have this constructor:

public FormTwo(String Data)
{
    InitializeComponent();
    viewData.text = Data;
}
It's the equivalent of doing this is C#, correct? Like the code I posted above? I create an instance of the subform, capture the string in the textbox of the parent form, and then using the constructor of the subform, set the variable.

You've done what no one on stackoverflow could do. They downvoted my question, and are voting on closing it. The reason? Too broad/good answers too long for this format! REALLY? On a coding forum!?!!? Stuff like this makes me think I should give up with stackoverflow anyway. I want to be/will later be a linux only user.

Thanks again for this.
 
Old 06-01-2015, 06:09 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
Glad to help you.
Hard to compare c# and python, but theoretically yes, the approach is the same: "using the constructor of the subform, set the variable".
If you really want to say thanks just click on YES.
 
1 members found this post helpful.
Old 06-01-2015, 05:38 PM   #5
Nexusfactor
Member
 
Registered: Jan 2015
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
Just out of curiosity

Is that the proper practice in Python, when passing data between forms, to call the subform constructor?

I'm asking, because in my research of Python, I understand certain aspects of OOP are not used in Python, for example, protected/private variables. Using properties and decorators instead of getter and setter methods.

So ideally, how would a python programmer tackle the problem of passing data from a parent form to a subform? If the above isn't typically how it's done.

Last edited by Nexusfactor; 06-01-2015 at 05:42 PM.
 
Old 06-02-2015, 12:50 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
hm. I would say you can use getter-setter in python too, that is a bit tricky (I mean to make a variable private), but not impossible.
When you create a new instance of an object (like subform) you can pass data using the constructor or by a special init function or also by setters, it depends on you. I would say it is quite similar in C++, java and python and c# of course.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Forms - no data saved or/and white screen Dafydd Programming 2 10-09-2011 03:23 PM
How to make data persist through webpages via Forms resetreset Programming 4 08-18-2010 12:55 PM
Passing form values between multiple forms!! AskMe Programming 5 09-07-2005 07:44 PM
passing an array from html-forms to php prabhatsoni Linux - Software 2 06-04-2005 12:11 AM
HTML, PHP, forms, submitting data Problem Silent1 Programming 3 08-31-2004 03:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration