Python窗体输入、展示并存储数据
利用python的tkinter进行窗体开发,可以实现输入框,以及利用get函数获取输入框的数据,并将这些数据存储到本地,如果利用数据库,也可以把窗体获取的数据导入到数据库中
from
tkinter
import
*
import
tkinter
.
messagebox
as
messagebox
root
=
Tk
(
)
root
.
title
(
"信息输入框"
)
root
.
geometry
(
'300x300'
)
input1
=
Label
(
root
,
text
=
"输入姓名:"
)
input1
.
pack
(
)
name_text
=
StringVar
(
)
name
=
Entry
(
root
,
textvariable
=
name_text
)
name_text
.
set
(
" "
)
name
.
pack
(
)
input2
=
Label
(
root
,
text
=
"输入年龄:"
)
input2
.
pack
(
)
age_text
=
StringVar
(
)
age
=
Entry
(
root
,
textvariable
=
age_text
)
age_text
.
set
(
" "
)
age
.
pack
(
)
input3
=
Label
(
root
,
text
=
"输入性别:"
)
input3
.
pack
(
)
gender_text
=
StringVar
(
)
gender
=
Entry
(
root
,
textvariable
=
gender_text
)
gender_text
.
set
(
" "
)
gender
.
pack
(
)
def
get_click
(
)
:
name
=
name_text
.
get
(
)
age
=
age_text
.
get
(
)
gender
=
gender_text
.
get
(
)
string
=
str
(
"名字:%s 年龄: %s 性别: %s "
%
(
name
,
age
,
gender
)
)
print
(
"名字:%s 年龄:åç %s 性别:%s "
%
(
name
,
age
,
gender
)
)
messagebox
.
showinfo
(
title
=
'aaa'
,
message
=
string
)
with
open
(
'测试数据写入.txt'
,
'a'
)
as
f
:
f
.
write
(
"%s\t%s\t%s\n"
%
(
name
,
age
,
gender
)
)
Button
(
root
,
text
=
"点击确定"
,
command
=
get_click
,
)
.
pack
(
)
Button
(
root
,
text
=
"点击结束"
,
command
=
exit
)
.
pack
(
)
root
.
mainloop
(
)
以上即利用Entry建立输入框,并给输入框命名,get函数读取到输入框的值,利用write函数将数据写入到本地的txt文件中