如何在python tkinter库中获取检查按钮的值,以便我可以知道用户是否选择了它?

问题描述

我创建了一个旅行表单,用户将在其中提交姓名、城市、性别、电话号码等。 我还创建了一个复选按钮,这样如果他想吃饭,他就可以在复选按钮上打勾。

如果用户勾选了如何获取Checkbutton的值 代码中写的用餐查询。 任何人都可以向我解释如何获得价值的逻辑 勾选按钮,如果用户已勾选它。

有什么方法可以让我使用 if 和 else 条件获取 CheckBox 的值

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()

    

解决方法

此代码有效:


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()

当我看到您的代码时,我发现您将变量 foodservicevalue 用作 IntVar() 和 Checkbutton。我已使用 if else 语句解决了您的问题。

,

这是@AmeyVijeesh 的代码,但我删除了 StringVar

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",relief="sunken")
f1.grid()

label = Label(f1,text="Welcome to travel agency",pady=5)
label.grid(row=0,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)

# 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)

# Creating Check Button #cHECKBUTTON

foodservicevalue = Checkbutton(text="Do you want any meals",padx=1)

# Button and packing with assiginn

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

root.mainloop()

使用 <tkinter.Entry>.get() 比创建 StringVar 并将它们分配给 <tkinter.Entry> 简单得多

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...