【问题标题】:Checking specific list entry and the rest of the list entries检查特定列表条目和其余列表条目
【发布时间】:2015-03-16 02:57:39
【问题描述】:

我正在制作数独游戏,我需要能够检查输入的数字是否已经在行中或列中是否存在冲突。现在,这正在改变所有种子值的背景。如何检查特定条目并查看它是否已经在行或列中?

  #nums_list is a 2_d list of the Entry widget id tags
  #check_list are the 0's and digits of the seed values
  def check_conf(self):
    for r in range(0, self.height, 1):
        for c in range(0, self.width, 1):
            if self.nums_list[r][c].get().isdigit():
                if int(self.nums_list[r][c].get()) == int(self.check_list[r][c]):
                    self.nums_list[r][c].config(background = 'red')

完整代码http://pastebin.com/Mmmh6JM4

【问题讨论】:

  • .get() 在做什么?
  • 我在 tkinter 中使用 9 x 9 的 Entry 小部件网格 .get() 正在获取 entry 小部件内的值
  • 为什么需要强制转换为 int 进行比较?不完全确定您的代码在做什么,但将可能的位置作为元组存储在字典中可能更容易
  • 您发布的代码有效吗?
  • 我发现我正在检查文件的种子值是否等于那些显然是正确的条目。我需要为所有当前条目创建一个新列表,并检查新条目是否已经在该特定行或列中

标签: python list loops tkinter multidimensional-array


【解决方案1】:

很高兴你明白了。

我会冒着对这个评论投反对票的风险回答:如果你做这样的事情,你可以让你的代码更具可读性:

#nums_list is a 2_d list of the Entry widget id tags
#check_list are the 0's and digits of the seed values
def check_conf(self):
    # iterate over rows
    for widgets, check_values in zip(self.nums_list, self.check_list):
        #iterate over columns
        for widget, check_value in zip(widgets, check_values):
            if widget.get().isdigit():
                if widget.get() == check_value:
                    widget.config(background = 'red')

【讨论】:

  • 错字(widget,不是 widgit)是故意的吗?
  • @badp ... 只是不注意我的名字,谢谢指出。
猜你喜欢
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2011-02-16
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
相关资源
最近更新 更多