马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 dragov 于 2024-10-28 17:28 编辑
from tkinter import *
root=Tk()
m=[{"designation":"北京万泉河","aspect":"hourglass"},{"designation":"天津海河","aspect":"error"},{"designation":"退出按钮","aspect":"info"}]
def tj():
p=Toplevel(root)
Button(p,text="这是天津的标签!").pack()
def bj():
q=Toplevel(root)
Button(q,text="这是北京的标签!").pack()
def secede():
root.destroy()
for i in range(len(m)):
n=Button(root,text=m[i]["designation"],bitmap=m[i]["aspect"],compound="bottom")
n.pack()
if "天津" in m[i]["designation"] :
n.config(command=tj)
if "北京" in m[i]["designation"] :
n.config(command=bj)
if "退出" in m[i]["designation"] :
n.config(command=root.destroy)
mainloop()
在程序体中的 3 个 if 语句,使用起来有些别扭,怎么修改呢 ?
from tkinter import *
root = Tk()
m = [
{"designation": "北京万泉河", "aspect": "hourglass"},
{"designation": "天津海河", "aspect": "error"},
{"designation": "退出按钮", "aspect": "info"}
]
def on_button_click(button_text):
if "天津" in button_text:
tj()
elif "北京" in button_text:
bj()
elif "退出" in button_text:
secede()
def tj():
p = Toplevel(root)
Button(p, text="这是天津的标签!").pack()
def bj():
q = Toplevel(root)
Button(q, text="这是北京的标签!").pack()
def secede():
root.destroy()
for item in m:
n = Button(root, text=item["designation"], bitmap=item["aspect"], compound="bottom")
n.pack()
n.config(command=lambda: on_button_click(item["designation"]))
root.mainloop()
|