import
tkinter as tk
'''松耦合'''
# 彈窗
class
MyDialog(tk.Toplevel):
def
__init__(
self
):
super
().__init__()
self
.title(
'設(shè)置用戶信息'
)
# 彈窗界面
self
.setup_UI()
def
setup_UI(
self
):
# 第一行(兩列)
row1
=
tk.Frame(
self
)
row1.pack(fill
=
"x"
)
tk.Label(row1, text
=
'姓名:'
, width
=
8
).pack(side
=
tk.LEFT)
self
.name
=
tk.StringVar()
tk.Entry(row1, textvariable
=
self
.name, width
=
20
).pack(side
=
tk.LEFT)
# 第二行
row2
=
tk.Frame(
self
)
row2.pack(fill
=
"x"
, ipadx
=
1
, ipady
=
1
)
tk.Label(row2, text
=
'年齡:'
, width
=
8
).pack(side
=
tk.LEFT)
self
.age
=
tk.IntVar()
tk.Entry(row2, textvariable
=
self
.age, width
=
20
).pack(side
=
tk.LEFT)
# 第三行
row3
=
tk.Frame(
self
)
row3.pack(fill
=
"x"
)
tk.Button(row3, text
=
"取消"
, command
=
self
.cancel).pack(side
=
tk.RIGHT)
tk.Button(row3, text
=
"確定"
, command
=
self
.ok).pack(side
=
tk.RIGHT)
def
ok(
self
):
self
.userinfo
=
[
self
.name.get(),
self
.age.get()]
# 設(shè)置數(shù)據(jù)
self
.destroy()
# 銷毀窗口
def
cancel(
self
):
self
.userinfo
=
None
# 空!
self
.destroy()
# 主窗
class
MyApp(tk.Tk):
def
__init__(
self
):
super
().__init__()
#self.pack() # 若繼承 tk.Frame ,此句必須有!
self
.title(
'用戶信息'
)
# 程序參數(shù)/數(shù)據(jù)
self
.name
=
'張三'
self
.age
=
30
# 程序界面
self
.setupUI()
def
setupUI(
self
):
# 第一行(兩列)
row1
=
tk.Frame(
self
)
row1.pack(fill
=
"x"
)
tk.Label(row1, text
=
'姓名:'
, width
=
8
).pack(side
=
tk.LEFT)
self
.l1
=
tk.Label(row1, text
=
self
.name, width
=
20
)
self
.l1.pack(side
=
tk.LEFT)
# 第二行
row2
=
tk.Frame(
self
)
row2.pack(fill
=
"x"
)
tk.Label(row2, text
=
'年齡:'
, width
=
8
).pack(side
=
tk.LEFT)
self
.l2
=
tk.Label(row2, text
=
self
.age, width
=
20
)
self
.l2.pack(side
=
tk.LEFT)
# 第三行
row3
=
tk.Frame(
self
)
row3.pack(fill
=
"x"
)
tk.Button(row3, text
=
"設(shè)置"
, command
=
self
.setup_config).pack(side
=
tk.RIGHT)
# 設(shè)置參數(shù)
def
setup_config(
self
):
# 接收彈窗的數(shù)據(jù)
res
=
self
.ask_userinfo()
#print(res)
if
res
is
None
:
return
# 更改參數(shù)
self
.name,
self
.age
=
res
# 更新界面
self
.l1.config(text
=
self
.name)
self
.l2.config(text
=
self
.age)
# 彈窗
def
ask_userinfo(
self
):
inputDialog
=
MyDialog()
self
.wait_window(inputDialog)
# 這一句很重要?。?!
return
inputDialog.userinfo
if
__name__
=
=
'__main__'
:
app
=
MyApp()
app.mainloop()