Tkinter and Getting Input

I am trying to get input from a tkinter window. I have the following code segment:

.
.
. 
    self.getPort=Tk()
    self.string=StringVar()
    textLabel=Label(self.getPort,text='HTTP Port').grid(row=0,column=0)
    textBox=Entry(self.getPort,textvariable=self.string).grid(row=0,column=1)
    button=Button(self.getPort,text='OK',command=self.getString()).grid(row=1,column=0,columnspan=2)
    self.getPort.mainloop()
.
.
.
def getString(self):
    print(self.string.get())

I’ve tried several variations but in this case the window does not display. In other cases the window displays but the command seems to get executed before the button is clicked. The objective is when the user clicks the button, get the input and close the window. Can someone help me through this? TIA.

Your code does not amount to something we can run. I am unsure what the structure is, but some things stand out. Definitely wrong:

This means textLabel will be None. Same applies to textBox and button. This is because grid() returns None.

The command here is set to the return of self.getString(). If you call a method or function, the parameters are evaluated before the call. Here you want to pass the method, so no parentheses, self.getString.

It is unlikely that you want self.getPort to be an instance of Tk. A Tk is the main window of your application, secondary windows, like dialogs, are instances of Toplevel. It looks like this is meant to be a dialog, so a Toplevel. Its events are handled by the mainloop of your main window, not a separate mainloop.

Thanks for the help. I was not aware of Toplevel as this is my first tkinter application. That and the other suggestions fixed everything.

   .
   .
   .
    self.getPort=Toplevel()
    self.string=StringVar()
    textLabel=Label(self.getPort,text='HTTP Port')
    textLabel.grid(row=0,column=0)
    textBox=Entry(self.getPort,textvariable=self.string)
    textBox.grid(row=0,column=1)
    button=Button(self.getPort,text='OK',command=self.getString)
    button.grid(row=1,column=0,columnspan=2)

.
.
.

def getString(self):
    print('returned=',self.string.get())
    self.getPort.destroy()