Python tkinter get () from another function

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 input fields if they are in another function? All input fields are in the CreateGUI () function. I thought of passing txtName variables, etc. To the Create () function. Is this method correct?

+4
source share
2 answers

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() 

Using the txtName self attribute, you can access its value in other methods using self.txtName . Other variables may be treated equally.

+3
source

It is useful to give Entry widgets an instance of StringVar to hold input.

  self.myStringVar = tk.StringVar() self.myEntryWidget = tk.Entry(self.myFrame,textvariable=self.myStringVar) self.myEntryWidget.grid(row=1,column=1) 

Then in some other function you call:

  self.myStringVar.get() 

This is all better in the classroom, as it simplifies GUI management.

+1
source

Source: https://habr.com/ru/post/1392468/


All Articles