【问题标题】:Python Tkinter checkbox to modify an existing list dictionary valuePython Tkinter 复选框用于修改现有列表字典值
【发布时间】:2016-04-04 23:09:55
【问题描述】:

我有一个格式如下的字典列表:

dict_list = [{'Contact Name' : 'Jeff Bezos', 'Email' : 'Jeff@Amazon.com', 'Send' : 0},
 {'Contact Name' : 'Bill Gates', 'Email' : 'Bill@Microsoft.com', 'Send' : 0}]

我目前使用 Tkinter 显示每个名称:

for eachclient in dict_list:
   contactLabel = Label(text='Contact Name: ' + eachclient['Contact Name'])     
   emailLabel = Label(text='Contact Email: ' + eachclient['Email'])

我需要一个 Tkinter 复选框,它可以修改 dict_list 中每个条目的“发送”值。
我还将添加一个发送按钮,该按钮仅在选中复选框时才会发送。
(发送按钮会调用类似的命令:)

   def buttonCheck():
       for eachclient in dict_list:
           if eachclient 'Send' = 1:
              send the message
           else:
              skip this client 

这可能吗?我非常感谢我能得到的任何帮助!

【问题讨论】:

  • 您的具体问题是什么?你那里有一些代码。它有什么问题?
  • 发送当前 = 0。我需要一个 Tkinter 复选框,选中后将值修改为 1。
  • 这个问题与stackoverflow.com/q/34456661/7432有何不同?
  • 你为什么不能只做dict_list[index]['Send'] = 1
  • 修改字典中的现有值与创建新字典并向其中添加值有很大不同。我非常感谢您之前提供的帮助,尽管 Bryan Oakley。

标签: python list dictionary tkinter


【解决方案1】:

复选框本身可能是for 循环的一部分,最后是:

checkbox = Button(text=str(check), command=checkemail, width=2)
# The str(check) part is for rows.

注意按钮本身必须在网格中,类似于此。

使用cget方法(解释见this,例子见this),复选框的功能可能是这样的:

def checkemail(): # Using an example function name
    x = int(checkbox.cget("text")) # Gets the row number (check)
    dict_list[x]['Send'] = 1 # Changes the value of 'Send' for the contact to 1

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2012-08-04
    • 2016-07-03
    • 2016-09-30
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多