def CreateGUI():
WinCreate=Toplevel(master)
WinCreate.attributes("-toolwindow",1)
WinCreate.resizable(0,0)
WinCreate.transient(master)
WinCreate.grab_set()
sideframe=Frame(WinCreate,bd=2,relief=GROOVE)
Label(WinCreate,text=" logbook ",fg="White",bg="#3b5998",font=("RoyalBavarian",20)).pack(side=TOP,fill=X)
Label(sideframe,text="Name: ",fg="Black",font=("Tahoma",12)).grid(row=0,column=0,sticky=E)
Label(sideframe,text="Age: ",fg="Black",font=("Tahoma",12)).grid(row=1,column=0,sticky=E)
Label(sideframe,text="Gender: ",fg="Black",font=("Tahoma",12)).grid(row=2,column=0,sticky=E)
Label(sideframe,text="Eye Color: ",fg="Black",font=("Tahoma",12)).grid(row=3,column=0,sticky=E)
txtName=Entry(sideframe)
txtAge=Entry(sideframe)
txtEye=Entry(sideframe)
txtName.grid(row=0,column=1)
txtAge.grid(row=1,column=1)
var=StringVar(sideframe)
var.set("Male")
optGender=OptionMenu(sideframe,var,"Male","Female")
optGender.grid(row=2,column=1)
txtEye.grid(row=3,column=1)
sideframe.pack(side=LEFT,pady=7,padx=7,ipady=3,ipadx=3)
rightside=Frame(WinCreate,height=116,width=283)
irighttop=Frame(rightside,bd=2,relief=GROOVE)
irightbottom=Frame(rightside,bd=2,width=274,height=50,relief=GROOVE)
Label(irighttop,text="Username: ",fg="Black",font=("Tahoma",12)).grid(row=0,column=0,sticky=E)
Label(irighttop,text="Password: ",fg="Black",font=("Tahoma",12)).grid(row=1,column=0,sticky=E)
Label(irighttop,text="Confirm Password: ",fg="Black",font=("Tahoma",12)).grid(row=2,column=0,sticky=E)
txtUsername=Entry(irighttop)
txtPassword=Entry(irighttop)
txtConPass=Entry(irighttop)
txtUsername.grid(row=0,column=1)
txtPassword.grid(row=1,column=1)
txtConPass.grid(row=2,column=1)
irighttop.pack(side=TOP,ipady=3,ipadx=3)
btnCreateFinal=Button(irightbottom,text="Create",fg="Black",command=Create(txtName,txtAge,txtEye,var,txtPassword,txtConPass,txtUsername))
btnCancel=Button(irightbottom,text="Cancel",fg="Black")
btnCreateFinal.pack(side=LEFT)
btnCancel.pack(side=RIGHT)
irightbottom.pack_propagate(0)
irightbottom.pack(side=BOTTOM,ipady=3,ipadx=3)
rightside.pack_propagate(0)
rightside.pack(side=RIGHT)
def Create(txtName,txtAge,txtEye,var,txtPassword,txtConPass,txtUsername):
choice='1'
password="blank"
conpassword="blank2"
#----------------------
name=txtName.get()
age=txtAge.get()
gender=var.get()
eye=txtEye.get()
password=txtPassword.get()
conpassword=txtConPass.get()
My question here is how can I get data from the entry boxes if they are in another function? All the entry boxes are in CreateGUI() function. I thought to pass the variables txtName etc to the Create() function. Is this method correct?
GUI programming is usually done inside a class:
class SimpleApp(object):
def createGUI(self):
...
self.txtName=Entry(sideframe)
self.txtName.grid(row=0,column=1)
def create(self):
...
name=self.txtName.get()
By making txtName
an attribute of self
, you can access its value in other methods with self.txtName
. The other variables can be handled the same way.