Reputation:
I'm trying to write a simple Tkinter form. It should read user's input, and then display the result when clicking a button.
However, my code doesn't return anything. I found this thread: Python Tkinter Entry get()
But still nothing I do returns me the text that users entered.
This is my script. Your help is appreciated:
import tkinter as tk
import tkinter.font as tkFont
class App:
def __init__(self, root):
# setting title
root.title("undefined")
# setting window size
global result
width = 600
height = 500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
LineEdit = tk.Entry(root)
LineEdit["borderwidth"] = "2px"
LineEdit.place(x=250, y=50, width=289, height=30)
result = LineEdit.get()
LineLabel = tk.Label(root)
LineLabel["text"] = "Enter Your Input:"
LineLabel.place(x=60, y=50, width=177, height=30)
GoButton = tk.Button(root)
GoButton["text"] = "Analyze"
GoButton.place(x=170, y=130, width=195, height=58)
GoButton["command"] = self.DisplayInput
def DisplayInput(self):
print(result)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
Upvotes: 0
Views: 412
Reputation: 47193
You should get the content of LineEdit
inside DisplayInput()
. But you need to change LineEdit
to instance variable self.LineEdit
, otherwise it cannot be accessed inside DisplayInput()
:
import tkinter as tk
import tkinter.font as tkFont
class App:
def __init__(self, root):
# setting title
root.title("undefined")
# setting window size
#global result
width = 600
height = 500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
self.LineEdit = tk.Entry(root)
self.LineEdit["borderwidth"] = "2px"
self.LineEdit.place(x=250, y=50, width=289, height=30)
#result = LineEdit.get()
LineLabel = tk.Label(root)
LineLabel["text"] = "Enter Your Input:"
LineLabel.place(x=60, y=50, width=177, height=30)
GoButton = tk.Button(root)
GoButton["text"] = "Analyze"
GoButton.place(x=170, y=130, width=195, height=58)
GoButton["command"] = self.DisplayInput
def DisplayInput(self):
result = self.LineEdit.get()
print(result)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
Upvotes: 1