-3

I am trying to design a GUI for a bit of fun. the purpose of the code is to take what you entered into a "entry widget" and print it out in the terminal using the print() command when you press the submit button.

Two questions.

One: i keep getting an "Invalid syntax" error in the code both the root.geometry() and root.mainloop()

root.geometry("300x300")

root.mainloop()

two: is there a more effective way of getting the text from a tkinter entry widget and storing it in a variable. i would like to be able to store data from multiple entry widgets to multiple variables, and how can i do that.

Here is my full code.

from tkinter import *

root = Tk()

def get_input():
    val = e1.get("1.0", "end-1-c")
    print(val)

lbl1 = Label(root, text = "Enter your name:").grid(row= "0", column= "0")
e1 = Entry(root, borderwidth = 5, width = 20).grid(row = "0", column = "1")
sbut = Button(root, text = "submit", command = get_input.grid(row= "1", column = "0")

root.geometry("300x300")

root.mainloop()

i used https://www.youtube.com/watch?v=FueIPFqRyyY&t=85s for the code to save entry widget data to a varible

Thanks for any help

ath0rus
  • 83
  • 1
  • 9
  • Read [AttributeError: NoneType object has no attribute ...](https://stackoverflow.com/a/1101765/7414759) – stovfl Feb 14 '20 at 09:35

1 Answers1

2

You should not define a widget and .grid() it in the same line. You also had an Invalid syntax because you forgot a ) at the line sbut = Button(....grid(row= "1", column = "0"))

You can try this :

from tkinter import *

root = Tk()

def get_input():
    val = e1.get()
    e1.delete("0", "end")
    print(val)

lbl1 = Label(root, text = "Enter your name:")
lbl1.grid(row= "0", column= "0")
e1 = Entry(root, borderwidth = 5, width = 20)
e1.grid(row = "0", column = "1")
sbut = Button(root, text = "submit", command = get_input)
sbut.grid(row= "1", column = "0")

root.geometry("300x300")

root.mainloop()
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
  • Thanks for your Help phoenixo. i will use your advice. – ath0rus Feb 14 '20 at 09:19
  • 1
    another advice for you : it is not advised to use `from tkinter import *`, you'd better use `import tkinter as tk` and then use `root = tk.Tk()`, `e1 = tk.Entry()` ... – Phoenixo Feb 14 '20 at 09:33