tkinter,请用户输入以指定要更新的标签

时间:2019-12-07 21:49:14

标签: python tkinter user-input tkinter-entry

我有46个配置有图像的标签。我希望用户将Entry标签输入要更改的标签,然后再更改标签图像。这是我所拥有的。这段代码将触发AttributeError:'str'对象没有属性'configure'

class Agilent(tk.Frame):

def __init__(self, master, **kw):
    super().__init__(master, **kw)
    self.master = master

    # Cell Update label plus text
    self.cell_update = tk.Label(self.master, text="Select Cell", font=('times', 14, 'bold'))
    self.cell_update.place(x=200, y=200)
    self.cell_update_entry = tk.Entry(self.master, font=('times', 14, 'bold'), width=32)
    self.cell_update_entry.place(x=400, y=200)

    # Takes a value
    self.brick = tk.Label(self.master, text="Brick Number", font=('times', 14, 'bold'))
    self.brick.place(x=460, y=600)
    self.brick_entry = tk.Entry(self.master, font=('times', 14, 'bold'), width=4)
    self.brick_entry.place(x=760, y=600)
    self.brick_entry.config(bd=4)

    # execute button
    self.change_cell_button = tk.Button(self.master, text="Change Cell", command=self.cell_color)
    self.change_cell.place(x=800, y=300)

    self.grey_cell = ImageTk.PhotoImage(Image.open(file))
    self.grey_cell_image = self.grey_cell

    self.orange_cell = ImageTk.PhotoImage(Image.open(file))       
    self.orange_cell_image = self.orange_cell

    self.red_cell = ImageTk.PhotoImage(Image.open(file))  
    self.red_cell_image = self.red_cell

    self.green_cell = ImageTk.PhotoImage(Image.open(file))            
    self.green_cell_image = self.green_cell

    # This is 1 of 46 options labeled cell_1.....cell_n
    self.cell_1 = tk.Label(self.master, image=self.grey_cell_image, background='white')
    self.cell_1.place(x=1460, y=20)


def cell_color(self):
    r = self.cell_update_entry.get()
    x = "self." + r                     # Build string to use as Label call
    y = int(self.brick_entry.get())

    if y < 1:
        self.cell_1.configure(image=self.orange_cell) # If I call the cell directly this works

    elif y % 2 == 0:
        x.configure(image=self.green_cell)#AttrinuteError: 'str' object has no attribute 'configure'

    else:
        x.configure(image=self.red_cell)#AttrinuteError: 'str' object has no attribute 'configure'

if __name__ == "__main__":

    root = tk.Tk()
    root.title("Wirebond Voltage Drop")
    root.geometry('1600x1200')
    root.resizable(False, False)

    dmm = Agilent(root)
    root.mainloop()

0 个答案:

没有答案