-1

I've created a travel form in which a user will submit name, city, gender, phone number etc. Also, I've created a check button so that a user wants a meal he can tick on the check button. My Question is How to get values of the Check button if the user has ticked the meal query written in the code. Can anyone explain to me the logic on how to get the value of the Check button if the user has ticked on it?

from tkinter import *

root = Tk()

root.geometry('800x800')



def b():
    print('Name is', namevalue.get()),
    print('Phone number  is', phonevalue.get())
    print('Gender is', gendervalue.get()),
    print('Extra Phone number is', phone1value.get()),
    print('City is', cityvalue.get())
    print('Food is required', )



f1 = Frame(root, bg = 'red', borderwidth = 8, relief = SUNKEN)
f1.grid()


Label(f1, text = 'Welcome to travel agency', pady = 5).grid(row = 0, column = 3)






#Making Widgets or we may call headers

name = Label(root, text = 'name')
phone = Label(root, text = 'Phone')
gender = Label(root, text = 'Enter your gender')
phone1 = Label(root, text = 'Extra Number')
city = Label(root, text = 'Your City')




name.grid(row = 1,column = 0)
phone.grid(row = 2,column = 0)
gender.grid(row = 3, column = 0)
phone1.grid(row = 4, column = 0)
city.grid(row = 5, column = 0)



#Assigining the headers a variable type

namevalue = StringVar()
phonevalue = StringVar()
gendervalue = StringVar()
phone1value = StringVar()
cityvalue = StringVar()
foodservicevalue = IntVar()






nameentry = Entry(root, textvariable = namevalue)
phoneentry = Entry(root, textvariable = phonevalue)
genderentry = Entry(root, textvariable = gendervalue)
cityentry = Entry(root, textvariable = cityvalue)
phone1entry = Entry(root, textvariable = phone1value)





nameentry.grid(row = 1, column = 3)
phoneentry.grid(row = 2, column = 3)
genderentry.grid(row = 3, column = 3)
phone1entry.grid(row = 4, column = 3)
cityentry.grid(row = 5, column = 3)




#Creating Check Button checkbutton

foodservicevalue = Checkbutton(text ='Do you wan\'t any meals', variable = foodservicevalue)
foodservicevalue.grid(row = 6, column = 3, padx = 1)


#Button and packing with assiginn

Button(text = 'Submit', command = b).grid(row = 7, column = 3)



root.mainloop()

    
  • Why are you using a `StringVar` for each one of those entries? Instead you can use `.get()`. That will remove half of the variables and make the program easier to debug – TheLizzard Apr 25 '21 at 11:20
  • Please try to reduce this code down to a [mcve]. If the question is about getting the value from a checkbutton, we don't need much more than the checkbutton, a function, a button to call the function, and enough extra code to make it work. – Bryan Oakley Apr 25 '21 at 16:03
  • Alright, Actually I'm learning it from scratch, so it will take time to reduce code efficiency. – blogs barrel Jul 12 '21 at 09:55

2 Answers2

1

This code works:


from tkinter import *

root = Tk()

root.geometry('800x800')


def b():
    print('Name is', namevalue.get()),
    print('Phone number  is', phonevalue.get())
    print('Gender is', gendervalue.get()),
    print('Extra Phone number is', phone1value.get()),
    print('City is', cityvalue.get())

    if food_required.get() == 1:
        print("Food is required.")

    elif food_required.get() == 0:
        print("Food is not required.")

    # When the check button is clicked, then the value is 1 and it can be get using the .get() function.
    # Similarly when the check button is not clicked then the value is 0.


f1 = Frame(root, bg='red', borderwidth=8, relief=SUNKEN)
f1.grid()

Label(f1, text='Welcome to travel agency', pady=5).grid(row=0, column=3)

# Making Widgets or we may call headers

name = Label(root, text='name')
phone = Label(root, text='Phone')
gender = Label(root, text='Enter your gender')
phone1 = Label(root, text='Extra Number')
city = Label(root, text='Your City')

name.grid(row=1, column=0)
phone.grid(row=2, column=0)
gender.grid(row=3, column=0)
phone1.grid(row=4, column=0)
city.grid(row=5, column=0)

# Assigining the headers a variable type

namevalue = StringVar()
phonevalue = StringVar()
gendervalue = StringVar()
phone1value = StringVar()
cityvalue = StringVar()
food_required = IntVar()

nameentry = Entry(root, textvariable=namevalue)
phoneentry = Entry(root, textvariable=phonevalue)
genderentry = Entry(root, textvariable=gendervalue)
cityentry = Entry(root, textvariable=cityvalue)
phone1entry = Entry(root, textvariable=phone1value)

nameentry.grid(row=1, column=3)
phoneentry.grid(row=2, column=3)
genderentry.grid(row=3, column=3)
phone1entry.grid(row=4, column=3)
cityentry.grid(row=5, column=3)

# Creating Check Button #cHECKBUTTON

foodservicevalue = Checkbutton(text='Do you wan\'t any meals', variable=food_required)
foodservicevalue.grid(row=6, column=3, padx=1)

# Button and packing with assiginn

Button(text='Submit', command=b).grid(row=7, column=3)

root.mainloop()

When I saw your code, I found that you have used the variable foodservicevalue as an IntVar() and Checkbutton. I have used the if else statements to fix your issue.

0

This is @AmeyVijeesh's code but I removed the StringVars:

from tkinter import *


def b():
    print("Name is", nameentry.get()),
    print("Phone number  is", phoneentry.get())
    print("Gender is", genderentry.get()),
    print("Extra Phone number is", phone1entry.get()),
    print("City is", cityentry.get())

    if food_required.get() == 1:
        print("Food is required.")
    elif food_required.get() == 0:
        print("Food is not required.")


root = Tk()
root.geometry("800x800")

f1 = Frame(root, bg="red", borderwidth=8, relief="sunken")
f1.grid()

label = Label(f1, text="Welcome to travel agency", pady=5)
label.grid(row=0, column=3)

# Making Widgets or we may call headers

name = Label(root, text="Name")
phone = Label(root, text="Phone")
gender = Label(root, text="Enter your gender")
phone1 = Label(root, text="Extra Number")
city = Label(root, text="Your City")

name.grid(row=1, column=0)
phone.grid(row=2, column=0)
gender.grid(row=3, column=0)
phone1.grid(row=4, column=0)
city.grid(row=5, column=0)

# Assigining the headers a variable type
food_required = IntVar()

nameentry = Entry(root)
phoneentry = Entry(root)
genderentry = Entry(root)
cityentry = Entry(root)
phone1entry = Entry(root)

nameentry.grid(row=1, column=3)
phoneentry.grid(row=2, column=3)
genderentry.grid(row=3, column=3)
phone1entry.grid(row=4, column=3)
cityentry.grid(row=5, column=3)

# Creating Check Button #cHECKBUTTON

foodservicevalue = Checkbutton(text="Do you want any meals", variable=food_required)
foodservicevalue.grid(row=6, column=3, padx=1)

# Button and packing with assiginn

button = Button(text="Submit", command=b)
button.grid(row=7, column=3)

root.mainloop()

Using <tkinter.Entry>.get() is much simpler than creating StringVars and assigning them to the <tkinter.Entry>s

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • Can you explain me what does nameentry = Entry(root) does and what if i write nameentry = Entry(root).grid(row = 0, column = 1). Why it doesn't works – blogs barrel Apr 26 '21 at 09:29
  • @blogsbarrel Loot at [this](https://stackoverflow.com/q/1101750/11106801). This is a very common question – TheLizzard Apr 26 '21 at 09:56