我已经在这里查看了多篇与标签/按钮/配置相关的相关文章,到目前为止还没有找到任何解释这个确切问题的文章。我之前已成功编写了基本的标签配置程序(使用'功能'以及使用' command ='以及' lambda'等)但它们只是使用set字符串在函数中包含配置代码(text =" String")。 我打算在此程序中执行的操作是将标签文本更改为“'”按钮中相同文本的标签文本,因此当用户单击该按钮时,标签会显示带有附加字符串的文本(& #34;联系信息:" +点击按钮
我确定问题在于我的功能代码'本身(特别是线条后半部分的格式化),或者创建'按钮。 ' for循环中的一行'下面可能需要编辑(或两者兼而有之)。
我猜测会有超过一条路线'考虑到所有'按钮文本'也包含在字典中作为键(名称),所以任何类型的建议,无论哪种方式都会非常感激,我是Python的新手,所以我确信这实际上是一个简单的简单问题。有经验的编码员
# The purpose of this program is to produce a GUI that acts as a 'contacts
#info retriever', when a 'contact' 'button' is clicked it displays the contact
#information of that person
# Make a 'dictionary' containing contact info
contacts = { 1: {"Name" : "Max", "Address" : "18 Fake Street, Hollywood", "Mobile" : "07542564872"},
2: {"Name" : "Brian", "Address" : "17 Fake Boulevard, Hollywood", "Mobile" : "07895465231"},
3: {"Name" : "Sally", "Address" : "19 Hill Valley, Hollywood", "Mobile" : "07956423145"},
4: {"Name" : "Steve", "Address" : "22 Fake House, Hollywood", "Mobile" : "07456213895"},
5: {"Name" : "Sara", "Address" : "46 Fake Drive, Hollywood", "Mobile" : "07652348192"},
6: {"Name" : "Kevin", "Address" : "18 Labyrinth Avenue, Hollywood", "Mobile" : "07563245196"},
7: {"Name" : "Paula", "Address" : "38 Fake Drive, Hollywood", "Mobile" : "07354695488"},
8: {"Name" : "Dave", "Address" : "Haystack Farm, Hollywood", "Mobile" : "07854623198"},
9: {"Name" : "Mary", "Address" : "Fake Valley, CA", "Mobile" : "07564289315"},
10:{"Name" : "Anastasia", "Address" : "Some Street, Hollywood", "Mobile" : "07546321595"}}
# Import 'GUI creation' module
import tkinter
# Create a 'window'
window = tkinter.Tk()
# Give the window a 'title'
window.title("Phonebook")
# Import an 'ico' file for icon
window.wm_iconbitmap("Moon Full.ico")
# Create a 'label' that displays opening instruction message at top
lbl = tkinter.Label(window, text="Click a contact to show their details:")
# 'Pack' the 'label'
lbl.pack()
# A 'function' for the 'reconfiguration' of 'labels' (changing their output)
def changeLabel():
lbl2.configure(text="Details for " + str(btn))
# 'Dynamically create' 'buttons' using a 'for loop' as so
for i in contacts:
#creates 'button' for what ever 'sub dictionary' (contact) the 'loop'
#is currently on
btn = tkinter.Button(window, text=contacts [i] ["Name"], command=changeLabel)
#'pack' the 'button'
btn.pack()
# Create a 'label' to display 'who the info belongs to'
lbl2 = tkinter.Label(window, text="No contact selected")
# Pack label
lbl2.pack()
# Draw the 'window', engage program
window.mainloop()
答案 0 :(得分:0)
在创建时将信息绑定到按钮的一种方法,并在单击按钮时将此信息作为参数传递:
contacts = { 1: {"Name" : "Max", "Address" : "18 Fake Street, Hollywood", "Mobile" : "07542564872"},
2: {"Name" : "Brian", "Address" : "17 Fake Boulevard, Hollywood", "Mobile" : "07895465231"},
3: {"Name" : "Sally", "Address" : "19 Hill Valley, Hollywood", "Mobile" : "07956423145"},
4: {"Name" : "Steve", "Address" : "22 Fake House, Hollywood", "Mobile" : "07456213895"},
5: {"Name" : "Sara", "Address" : "46 Fake Drive, Hollywood", "Mobile" : "07652348192"},
6: {"Name" : "Kevin", "Address" : "18 Labyrinth Avenue, Hollywood", "Mobile" : "07563245196"},
7: {"Name" : "Paula", "Address" : "38 Fake Drive, Hollywood", "Mobile" : "07354695488"},
8: {"Name" : "Dave", "Address" : "Haystack Farm, Hollywood", "Mobile" : "07854623198"},
9: {"Name" : "Mary", "Address" : "Fake Valley, CA", "Mobile" : "07564289315"},
10:{"Name" : "Anastasia", "Address" : "Some Street, Hollywood", "Mobile" : "07546321595"}}
import tkinter
window = tkinter.Tk()
window.title("Phonebook")
lbl = tkinter.Label(window, text="Click a contact to show their details:")
lbl.pack()
def changeLabel(text):
"""A 'function' for the 'reconfiguration' of 'labels'
(changing their output)
"""
lbl2.configure(text="Details for " + text)
for i in contacts:
btn = tkinter.Button(window, text=contacts[i]["Name"], command=lambda text=contacts[i]["Name"]:changeLabel(text))
btn.pack()
lbl2 = tkinter.Label(window, text="No contact selected")
lbl2.pack()
window.mainloop()
@BrianOakley在评论中的建议:由于联系人是全局的,如果代码将索引传递给函数而不是具有复杂的lambda语句,则代码可能更容易调试和维护。 (请参阅下面的代码,了解此替代方案,可以说是更强大的解决方案)
import tkinter
window = tkinter.Tk()
window.title("Phonebook")
lbl = tkinter.Label(window, text="Click a contact to show their details:")
lbl.pack()
def changeLabel(ndx):
"""A 'function' for the 'reconfiguration' of 'labels'
(changing their output)
"""
lbl2.configure(text="Details for " + contacts[ndx]["Name"])
for i in contacts:
btn = tkinter.Button(window, text=contacts[i]["Name"], command=lambda ndx=i: changeLabel(ndx))
btn.pack()
lbl2 = tkinter.Label(window, text="No contact selected")
lbl2.pack()
window.mainloop()