Article Directory
1. Enter the name
code show as below:
#输入名字
Label(F1,text='你的名字?').pack()
nameVar = StringVar()
nameVar.set("名字")
#输入 Entry
Entry(F1,textvariable=nameVar,width=15).pack()
2. Enter age
code show as below:
#输入年龄
def Digital(content):
if content.isdigit() or content =='':
return True
else
return False
# 将函数Digital包装成app里的dig_in 必须要做的
dig_in = app.register(Digital)
Label(F1,text='你的年龄?').pack()
ageVar = StringVar()
ageVar.set("18")
#设置只允许数字输入
#%P进行实时监测
Entry(F1,textvariable=ageVar,width=15,validate='key',validatecommand=(dig_in,'%P')).pack()
Click: Detailed effect display of attributes used in Entry
:
Three, single-select Radiobutton
code show as below:
#单选
langs = ['Python','Java','C/C++','Go','PHP','Ruby','Node.js']
Label(F1,text='你最喜欢哪一门编程语言? 单选').pack()
bestVar = IntVar() #声明bestVar 是tkinter里面的一个整型变量
bestVar.set(0) #默认喜欢第0个
#把所有radiobutton加进来
for i in range(len(langs)):
Radiobutton(F1,text=langs[i],variable=bestVar,value=i).pack()
In order to achieve its "single selection" behavior, ensure that the variable options of all buttons in a group are usedThe same variableAnd use the value option to specify what value each button represents.
Modify the code to align it to the left
for i in range(len(langs)):
Radiobutton(F1,text=langs[i],variable=bestVar,value=i).pack(anchor=W)
Click: Detailed explanation of the attributes used in Radiobutton
Four, multi-select Checkbutton
code show as below:
#多选
Label(F1,text='你想学习哪些编程语言? 多选').pack()
selectVar = []
for lang in langs:
selectVar.append(IntVar())
Checkbutton(F1,text=lang,variable=selectVar[-1]).pack(anchor=W)
Click: Detailed explanation of the attributes used in the Checkbutton
Five, submit and exit buttons
def callback():
name = nameVar.get()
age = ageVar.get()
best = langs[bestVar.get()]
selects = []
for i in range(len(selectVar)):
# 0 选 1 没选
if selectVar[i].get() == 1:
selects.append(langs[i])
string ='你的名字:'+name+'\n'
string +='你的年龄:'+age+'\n'
string +='你最喜欢哪一门编程语言+'+best+'\n'
string +='你最想学的编程语言:'+''.join(selects)+'\n'
messagebox.showinfo('确认你的信息',string)
nameVar.set('')
ageVar.set('')
#按键
Button(F1,text='提交',command=callback).pack(side=LEFT,padx='10',pady='10')
Button(F1,text='退出',command=app.quit).pack(side=RIGHT,padx='10',pady='10')
to sum up
Question 1: IntVar() and StgringVar
are both owned by tkinter
IntVar() in tk represents an integer value, StringVar represents a variable value
For example, a=IntVar() means to tell the compiler that a is an integer. It is
like declaring a variable in python and assigning it to a number. For example, a = 123 means a is an integer
v[-1] #表示列表的最后一个元素.
v=[1,2,3,4]
v[-1] == 4
v[-2] == 3