如何使用二维列表中的值作为函数中的参数

时间:2018-08-21 08:16:35

标签: python function class tkinter

我正在尝试使用二维列表中的值作为该函数的参数,该函数用于GUI中的大多数按钮。当我调用add_to_display函数并单击其中一个按钮时,显示条目将显示".!frame.!button16"而不是按钮列表中的字符串值之一。

from tkinter import * 

class Functions():        
    def replace_text(self, text):
        self.display.delete(0, END)
        self.display.insert(0, text)

    def add_to_display(self, text, *args):
        self.user_input = self.display.get()
        self.text_length = len(self.user_input)

        if self.user_input == "0":
           self.replace_text(text)
        else: 
           self.display.insert(self.text_length, text)


class Calculator(Functions):
   def __init__(self, root):
      frame = ttk.Frame(root)
      frame.grid(row = 0, column = 0)        

      self.display = Entry(frame, font=("Helvetica", 14), justify = "right")
      self.display.insert(0, "0")
      self.display.grid(column = 0, row = 0, columnspan = 4, sticky = "NSWE", padx = 10, pady = 10)

      self.button_list = [["7", "8", "9", "+"],
                        ["4", "5", "6", "-"],
                        ["1", "2", "3", "x"],
                        ["C", "0", ".", "/"]]

      for row_ in range(len(self.button_list)):
          for column_ in range(len(self.button_list[row_])):
              self.button_list[row_][column_] = Button(frame, font= ("Helvetica", 12), text = self.button_list[row_][column_],
                                                     command = lambda: self.add_to_display(self.button_list[row_][column_]))
              self.button_list[row_][column_].grid(row = row_ + 1 , column = column_, sticky = "NSWE")


#Main Program       
root = Tk() 
root.title("Calculator Trial 2")
App = Calculator(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

问题出在电话上

self.button_list[row_][column_] = Button(frame, font= ("Helvetica", 12), text = self.button_list[row_][column_], command = lambda: self.add_to_display(self.button_list[row_][column_]))

此行有两个问题:

  1. lambda函数在覆盖变量self.button_list [row _] [column_]之后执行,因此该变量不再是您想要的原始字符串

  2. 此执行顺序还会导致row_和column_变量都为3(因此将按钮保存在另一个列表中将无法解决您的问题)

我建议的解决方案是使用来自functools模块的局部函数。它对我有用。

from tkinter import *
from functools import partial

class Functions():        
    def replace_text(self, text):
        self.display.delete(0, END)
        self.display.insert(0, text)

    def add_to_display(self, text, *args):
        self.user_input = self.display.get()
        self.text_length = len(self.user_input)

        if self.user_input == "0":
           self.replace_text(text)
        else: 
           self.display.insert(self.text_length, text)


class Calculator(Functions):
    def __init__(self, root):
        frame = Frame(root)
        frame.grid(row = 0, column = 0)        

        self.display = Entry(frame, font=("Helvetica", 14), justify = "right")
        self.display.insert(0, "0")
        self.display.grid(column = 0, row = 0, columnspan = 4, sticky = "NSWE", padx = 10, pady = 10)

        self.button_list = [["7", "8", "9", "+"],
                        ["4", "5", "6", "-"],
                        ["1", "2", "3", "x"],
                        ["C", "0", ".", "/"]]

        for row_ in range(len(self.button_list)):
            for column_ in range(len(self.button_list[row_])):
                self.button_list[row_][column_] = Button(frame, font= ("Helvetica", 12), text = self.button_list[row_][column_],
                                                     command = partial(self.add_to_display, self.button_list[row_][column_]))
                self.button_list[row_][column_].grid(row = row_ + 1 , column = column_, sticky = "NSWE")


#Main Program       
root = Tk() 
root.title("Calculator Trial 2")
App = Calculator(root)
root.mainloop()

我应该添加一些内容:

  1. 请注意,ttk.Frame调用应触发错误。
  2. 即使在这种情况下,即使异常/错误的缩进都不会导致错误,也建议在每个脚本中使用统一的缩进

祝你好运!