tkitner按钮的颜色变化导致输出混乱

2024-09-25 04:25:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我以前问过这个问题,但没有成功的答案,所以我会再试一次

下面是按下按钮时改变按钮背景颜色的代码。它基本上取“eleNum”这个数字,然后用它打印出4个0到40的按钮面板

这是奇怪的一点。4号面板工作正常,但其他面板只是给我一个错误。例如,如果我按下面板2中的一个按钮:

line 113, in chooser
   self.Buttons2[index].onfigure(bg="orange")
AttributreError: 'int' object has no attribute 'configure'

这是我的密码

def floorChooserButtons( self, eleNum, floors, yStart, yEnd, xStart, xEnd):
    self.Buttons1 = [i for i in range(41)]
    self.Buttons2 = [i for i in range(41)]
    self.Buttons3 = [i for i in range(41)]
    self.Buttons4 = [i for i in range(41)]
    self.eleNumber = [i for i in range(4)]
    if(eleNum is 1):
        self.eleNumber[0] = tk.Label(self, width = 12, text="Elevator 1")
        self.eleNumber[0].grid(row = xStart-1, column =yStart+1, columnspan=3)
        xPos = xStart
        yPos = yStart 
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons1[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons1[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
    elif(eleNum is 2):
        self.eleNumber[1] = tk.Label(self, width = 12, text="Elevator 2")
        self.eleNumber[1].grid(row = xStart-1, column =yStart+1, columnspan=3)
        xPos = xStart
        yPos = yStart
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons2[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons2[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
    elif(eleNum is 3):
        self.eleNumber[2] = tk.Label(self, width = 12, text="Elevator 3")
        self.eleNumber[2].grid(row = xStart-1, column =yStart+1, columnspan=3)
        xPos = xStart
        yPos = yStart
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons3[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons3[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
    elif(eleNum is 4):
        self.eleNumber[3] = tk.Label(self, width = 12, text="Elevator 4")
        self.eleNumber[3].grid(row = xStart-1, column =yStart+1, columnspan=3)
        xPos = xStart
        yPos = yStart
        for floor in floors:
            if(yPos == yEnd):
                xPos = xPos + 1
                yPos = yStart
            if(xPos == xEnd-1):
                yPos = yStart+2
            self.Buttons4[floor] = tk.Button(self, width=3, text=floor, 
                command = lambda f=floor: self.chooser(f, eleNum))
            self.Buttons4[floor].grid(row=xPos, column =yPos)
            yPos = yPos + 1
        self.QUIT = tk.Button(self, text="QUIT", fg="red",
            command=root.destroy).grid(row = xPos, column = yPos)


def chooser(self, index, eleNum):
    print("Number", index, "pressed in elevator", eleNum)
    if eleNum is 1:
        self.Buttons1[index].configure(bg="blue")
    if eleNum is 2:
        self.Buttons2[index].configure(bg="orange")
    if eleNum is 3:
        self.Buttons3[index].configure(bg="pink")
    if eleNum is 4:
        self.Buttons4[index].configure(bg="red")

Tags: textinselfforiftkgridrow
1条回答
网友
1楼 · 发布于 2024-09-25 04:25:59

列表中有整数。你自己把它们放在那里,例如:self.Buttons2 = [i for i in range(41)]。在此之后,您只将其中的一些更改为按钮,例如:self.Buttons2[floor] = tk.Button( ...

然后尝试对整数调用configure方法—错误消息非常直接地解释了这一点。显然整数没有configure方法

因此,您的代码将始终只适用于一个面板。在使用给定的eleNum调用floorChooserButtons之后,所有列表都被重置为整数,只有与eleNum对应的列表被按钮填充(仍然,仅适用于floors列表中的索引)

快速解决方法是移动零件:

self.Buttons1 = [i for i in range(41)]
self.Buttons2 = [i for i in range(41)]
self.Buttons3 = [i for i in range(41)]
self.Buttons4 = [i for i in range(41)]

给建造师。然后,在为所有面板调用floorChooserButtons之后,您将用实际的按钮而不是数字填充它们。你的整个构造对我来说仍然没有意义(为什么你会在列表中有整数,并打算在其中存储按钮?)

作为补充说明,您错误地使用了is运算符。它在这里意外地起作用,因为Python中的整数是不可变的,但可读性不强。对于值相等测试,应该使用==运算符

相关问题 更多 >