Assigning entry widget value in a dialog box from other module
Jørgen Cederberg
jorgencederberg at hotmail.com
Wed May 7 02:50:34 EDT 2003
Python-lover wrote:
> Hi,
> Iam using python and tkinter. My program run number
> of lines. To increase the maintainablity i divided my
> program in to two modules (ie, 2 python programs) and
> imported them at needed place.
>
Hi,
think of the seperate files as modules. I really think you read some of
the introductory material found at http://www.python.org/doc/Newbies.html
> My first program called "a.py" is the main program
> in which i placed a button. When u click the button a
> dialogbox which is defined in "b.py" will be poped up.
> The dialog box contains two entry widgets. Now the
> problem is i want to set some default values for the
> entry widgets when they displayed. I defined 2
> variables called "name" ,"eno" in "a.py" and want to
> assign their values to corresponding entry widgets.
> When i run the program i got the following error:
>
> File "a.py" line 12, in ?
> import b
> File "b.py" line 4
> from import a
>
The problem is that you import module b in module a and module imports
a. This is cirular referencing and _not_ a good thing, nor a good practice.
> My program is given below.
I have told you this before, you indentation is all screwed up. What
editor are you using and how are you even able to run the files?
>
> ##########################################
> ############# a.py
> ##########################################
>
> from Tkinter import *
> import b
>
> class App:
> def __init__(self,parent):
> self.myparent = parent
> Button(self.myparent,text="Click",width=10,
> height=10,command=self.showdlg).pack()
> self.name = "AAAAA"
> self.no = 100
>
> def showdlg(self):
> b.MyDialog(self.myparent)
How about passing the instance of the App itself to the dialog like:
def showdlg(self):
b.MyDialog(self.myparent, self)
>
> root = Tk()
> myapp = App(root)
> root.mainloop()
>
> ##########################################
> ############# b.py
> ##########################################
>
> import tkMessageBox
> import tkSimpleDialog
> from Tkinter import *
> import a
You don't need to import a here.
> import string
>
> class MyDialog(tkSimpleDialog.Dialog):
here you could insert a __init__ method:
def __init__(self, parent, app, *args, **kwargs):
self.app = app
tkSimpleDialog.Dialog.__init__(self, parent, *args, **kwargs)
>
> def body(self, master):
>
> Label(master, text="First:").grid(row=0)
> Label(master, text="Second:").grid(row=1)
>
> self.e1 = Entry(master)
> self.e2 = Entry(master)
>
> self.e1.grid(row=0, column=1)
> self.e2.grid(row=1, column=1)
> self.e1.insert(0,a.myapp.name)
> self.e2.insert(0,a.myapp.no)
Here you should use the self.app variable created at initialization. Like:
self.e1.insert(0, self.app.name)
>
> def apply(self):
>
> first = (self.e1.get()) #string.atoi
> second = string.atoi(self.e2.get())
Instead of string.atoi you could use the int method
second = int(self.e2.get())
> print first, second # or something
>
Which version of Python are you using?
Below are the corrected files:
##########################################
############# a.py
##########################################
from Tkinter import *
import b
class App:
def __init__(self,parent):
self.myparent = parent
Button(self.myparent,text="Click",width=10,
height=10,command=self.showdlg).pack()
self.name = "AAAAA"
self.no = 100
def showdlg(self):
b.MyDialog(self.myparent)
root = Tk()
myapp = App(root)
root.mainloop()
##########################################
############# b.py
##########################################
import tkMessageBox
import tkSimpleDialog
from Tkinter import *
class MyDialog(tkSimpleDialog.Dialog):
def __init__(self, parent, app, *args, **kwargs):
self.app = app
tkSimpleDialog.Dialog.__init__(self, parent, *args, **kwargs)
def body(self, master):
Label(master, text="First:").grid(row=0)
Label(master, text="Second:").grid(row=1)
self.e1 = Entry(master)
self.e2 = Entry(master)
self.e1.grid(row=0, column=1)
self.e2.grid(row=1, column=1)
self.e1.insert(0, self.app.name)
self.e2.insert(0, self.app.no)
def apply(self):
first = self.e1.get()
second = int(self.e2.get())
print first, second # or something
Hope this helps
Jorgen Cederberg
More information about the Python-list
mailing list