我想检索并显示这个基本的python GUI形式的值。如何检索单选按钮和复选框值并显示它?这是代码


from tkinter import *
from tkinter import Button, Entry, Label, messagebox, ttk
top = Tk()
top.geometry("400x600")

在这里,不检索性别和复选框值。我如何从另一个名为genSelect()的函数获得单选按钮值?

def submit():
namevar = e1.get()
emailvar = e2.get()
phonevar = e3.get()
gen1=print(genSelect.gen)
h1=c1.get()
h2=c2.get()
h3=c3.get()
h4=c4.get()
h5=c5.get()
con=country.get()
st=states.get()
messagebox.showinfo("Your details", "Your Name is: "+namevar+"n Email: "+emailvar+"n Phone: "+phonevar+"n Gender: "+gen1+
"n Hobbys: "+h1+","+h2+","+h3+","+h4+","+h5+"n Country: "+con+"n State"+st)

我创建了另一个函数来获取性别值。我应该做同样的得到复选框的值吗?

def genSelect():
choice = var.get()
if choice == 1:
genSelect.gen = "Female"
elif choice == 2:
genSelect.gen = "Female"
else:
genSelect.gen = "Invalid selection"

#这是为三个部分展开的表单。bhdgchnxmiq, ijsjqijxiaihuxhwjxnwjx

var = IntVar()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()
register = Label(top, text="Register Form", font={})
register.place(x=120, y=5)
lb1 = LabelFrame(top, text="Personal Details")
lb1.place(height=160, width=390, x=5, y=35)
name = Label(lb1, text="Name")
name.place(x=50, y=20)
email = Label(lb1, text="Email")
email.place(x=50, y=50)
phone = Label(lb1, text="Phone no.")
phone.place(x=50, y=80)
gender = Label(lb1, text="Gender")
gender.place(x=50, y=110)
e1 = Entry(lb1)
e1.place(x=120, y=20)
e2 = Entry(lb1)
e2.place(x=120, y=50)
e3 = Entry(lb1)
e3.place(x=120, y=80)
R1 = Radiobutton(lb1, text="Male", variable="var", value="1")
R1.place(x=120, y=110)
R2 = Radiobutton(lb1, text="Female", variable="var", value="2")
R2.place(x=180, y=110)
lb2 = LabelFrame(top, text="Hobby")
lb2.place(height=70, width=390, x=5, y=200)
c1 = Checkbutton(lb2, text="Sports", variable=CheckVar1, onvalue=1, offvalue=0)
c1.place(x=5, y=12)
c2 = Checkbutton(lb2, text="Music", variable=CheckVar2, onvalue=1, offvalue=0)
c2.place(x=80, y=12)
c3 = Checkbutton(lb2, text="Cooking", variable=CheckVar3, onvalue=1, offvalue=0)
c3.place(x=155, y=12)
c4 = Checkbutton(lb2, text="Reading Books", variable=CheckVar4, onvalue=1, offvalue=0)
c4.place(x=230, y=12)
c5 = Checkbutton(lb2, text="Gaming", variable=CheckVar5, onvalue=1, offvalue=0)
c5.place(x=305, y=12)
lb3 = LabelFrame(top, text="Address")
lb3.place(height=205, width=390, x=5, y=277)
country_name = Label(lb3, text="Country")
country_name.place(x=10, y=8)
state_name = Label(lb3, text="State")
state_name.place(x=10, y=38)
country = ttk.Combobox(lb3, values=
[
"INDIA",
"Australia",
"Russia",
"Egypt",
"U.S.A",
"Canada",
"Argentina"
])
country.current(0)
country.place(x=65, y=8)
states = Listbox(lb3, selectmode=SINGLE, height=8, width=20)
states.insert(1, "Maharashtra")
states.insert(2, "Madhya Pradesh")
states.insert(3, "Kerala")
states.insert(4, "Orissa")
states.insert(5, "Punjab")
states.insert(6, "Rajast`enter code here`han")
states.insert(7, "Assam")
states.insert(8, "Gujarat")
states.place(x=65, y=42)
b1 = Button(top, command=submit, text="Submit", font={"arial", 12}, relief="solid")
b1.place(x=150, y=520)
top.mainloop()

输出

输入

整个代码:

from tkinter import *
from tkinter import Button, Entry, Label, messagebox, ttk
top = Tk()
top.geometry("400x600")
def submit():
namevar = e1.get()
emailvar = e2.get()
phonevar = e3.get()
gen1=genSelect()
if CheckVar1.get() == 1:
h1 = 'Sports'
else:
h1 = ''
if CheckVar2.get() == 1:
h2 = 'Music'
else:
h2= ''
if CheckVar3.get() == 1:
h3 = 'Cooking'
else:
h3 = ''
if CheckVar4.get() == 1:
h4 = 'Reading Books'
else:
h4 = ''
if CheckVar5.get() == 1:
h5 = 'Gaming'
else:
h5 = ''
con=country.get()
for i in states.curselection():
state = states.get(i)
print(state)

# st=states.get()
messagebox.showinfo("Your details", "Your Name is: "+namevar+"n Email: "+emailvar+"n Phone: "+str(phonevar)+"n Gender: "+gen1+
"n Hobbys: "+h1+","+h2+","+h3+","+h4+","+h5+"n Country: "+con+"n State"+state)

var = IntVar()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()
def genSelect():
choice = var.get()
if choice == 1:
gen = "Male"
elif choice == 2:
gen = "Female"
else:
gen = "Invalid selection"

return gen

register = Label(top, text="Register Form", font={})
register.place(x=120, y=5)
lb1 = LabelFrame(top, text="Personal Details")
lb1.place(height=160, width=390, x=5, y=35)
name = Label(lb1, text="Name")
name.place(x=50, y=20)
email = Label(lb1, text="Email")
email.place(x=50, y=50)
phone = Label(lb1, text="Phone no.")
phone.place(x=50, y=80)
gender = Label(lb1, text="Gender")
gender.place(x=50, y=110)
e1 = Entry(lb1)
e1.place(x=120, y=20)
e2 = Entry(lb1)
e2.place(x=120, y=50)
e3 = Entry(lb1)
e3.place(x=120, y=80)
R1 = Radiobutton(lb1, text="Male", variable=var, value="1")
R1.place(x=120, y=110)
R2 = Radiobutton(lb1, text="Female", variable=var, value="2")
R2.place(x=180, y=110)
lb2 = LabelFrame(top, text="Hobby")
lb2.place(height=70, width=390, x=5, y=200)
Checkbutton(lb2, text="Sports", variable=CheckVar1, onvalue=1, offvalue=0).place(x=5, y=12)
Checkbutton(lb2, text="Music", variable=CheckVar2, onvalue=1, offvalue=0).place(x=80, y=12)
Checkbutton(lb2, text="Cooking", variable=CheckVar3, onvalue=1, offvalue=0).place(x=155, y=12)
Checkbutton(lb2, text="Reading Books", variable=CheckVar4, onvalue=1, offvalue=0).place(x=230, y=12)
Checkbutton(lb2, text="Gaming", variable=CheckVar5, onvalue=1, offvalue=0).place(x=305, y=12)
lb3 = LabelFrame(top, text="Address")
lb3.place(height=205, width=390, x=5, y=277)
country_name = Label(lb3, text="Country")
country_name.place(x=10, y=8)
state_name = Label(lb3, text="State")
state_name.place(x=10, y=38)
country = ttk.Combobox(lb3, values=
[
"INDIA",
"Australia",
"Russia",
"Egypt",
"U.S.A",
"Canada",
"Argentina"
])
country.current(0)
country.place(x=65, y=8)
states = Listbox(lb3, selectmode=SINGLE, height=8, width=20)
states.insert(1, "Maharashtra")
states.insert(2, "Madhya Pradesh")
states.insert(3, "Kerala")
states.insert(4, "Orissa")
states.insert(5, "Punjab")
states.insert(6, "Rajast`enter code here`han")
states.insert(7, "Assam")
states.insert(8, "Gujarat")
states.place(x=65, y=42)
b1 = Button(top, command=submit, text="Submit", font={"arial", 12}, relief="solid")
b1.place(x=150, y=520)
top.mainloop()

最新更新