0

I am having trouble gaining the outputs from this form and i cant seem to identify where its going wrong.

from tkinter import *

def Button_to_text():
    firstname_info = firstname.get()
    lastname_info = lastname.get()
    age_info = age.get()
    print(firstname_info,lastname_info,age_info) 

screen = Tk()
screen.geometry("500x500")
screen.title("python_form")
heading = Label(text = "Demo Form",bg = "orange", fg="black",width = "500") 
heading.pack()

firstname_text = Label(text="firstname")
lastname_text = Label(text="lastname")
age_text = Label(text="age")
firstname_text.place(x=60, y= 40)
lastname_text.place(x=60,y=80)
age_text.place(x=60,y=120)

firstname = StringVar()
lastname = StringVar()
age = IntVar()

firstname_entry = Entry(textvariable = firstname)
lastname_entry = Entry(textvariable = lastname)
age_entry = Entry(textvariable = age)

firstname_entry.place(x=160, y=40)
lastname_entry.place(x=160,y=80)
age_entry.place(x=160,y=120)

register = Button(text = "register", width= "30",height ="2", command = Button_to_text())
register.place(x=50,y=290)

I followed a tutorial and my computing teacher cant help because he doesn't know python. plus my friends cant seem to identify the issue there are also no errors coming up so I know its a logic error and i also cant work out how to step so i can check variables
thanks to anyone who can help.

  • 1
    Welcome to Stackoverflow! To help others achieving a better solution for your problem please share more context about your issue on your post. What are you trying to do? – Gangula Apr 10 '20 at 11:03
  • 1
    Does this answer your question? [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – jizhihaoSAMA Apr 10 '20 at 11:47

1 Answers1

1

There are two problems with your code:

  1. You have to use a mainloop to keep the window being displayed continuously.

  2. You should not use the parentheses() while passing any function to a Button as an argument.

Note: And if the function has its own parameters then you will have to use lambda while passing it to a Button. But in your case, you can simply remove the parentheses().

Here's the fixed code:

from tkinter import *


def Button_to_text():
    firstname_info = firstname.get()
    lastname_info = lastname.get()
    age_info = age.get()
    print(firstname_info, lastname_info, age_info)


screen = Tk()
screen.geometry("500x500")
screen.title("python_form")
heading = Label(text="Demo Form", bg="orange", fg="black", width="500")
heading.pack()

firstname_text = Label(text="firstname")
lastname_text = Label(text="lastname")
age_text = Label(text="age")
firstname_text.place(x=60, y=40)
lastname_text.place(x=60, y=80)
age_text.place(x=60, y=120)

firstname = StringVar()
lastname = StringVar()
age = IntVar()

firstname_entry = Entry(textvariable=firstname)
lastname_entry = Entry(textvariable=lastname)
age_entry = Entry(textvariable=age)

firstname_entry.place(x=160, y=40)
lastname_entry.place(x=160, y=80)
age_entry.place(x=160, y=120)

register = Button(text="register", width="30", height="2", command=Button_to_text)
register.place(x=50, y=290)

screen.mainloop()

Note:

  1. As a good practice, you should always use small letters in the names of functions like this: def button_to_text():.

  2. And you should always import tkinter as tk instead of importing all * from tkinter. It is always a good practice. The only change you will need to do in the program is that you will need to use tk. before each item belonging to tkinter. Like this: screen = tk.Tk()

DaniyalAhmadSE
  • 807
  • 11
  • 20
  • 1
    amazing thank you I am aware of certain practices (block caps for constants) but I was unaware of the lowercase for functions, Thank you for highlighting this for me. – Ben Green Apr 11 '20 at 08:14
  • @BenGreen Please make sure to accept the answer if it worked for you, its been long but still :) – DaniyalAhmadSE Aug 07 '21 at 14:17