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文件中
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
