在python GUI中更改按钮颜色

时间:2014-09-11 15:42:09

标签: python button user-interface colors background

import sys
from tkinter import *

def run_GUI():
    # create the window
    root = Tk()
    frame = Frame(root)
    frame.pack()

    #modify root window
    root.title("Simple GUI")
    root.geometry("700x300") # w x h        
    def SP2T():     # Edit occurs here where I forgot to pit that the button was created in a called funtction
        #Creates Row
        frameSP2T = Frame(root)
        frameSP2T.pack(side = TOP)

        #Creating Buttons First Row
        button1 = Button(frameSP2T, padx=13, pady = 6, bd=4, text="SW02",fg = "black", command = SW02)
        button1.pack(side = LEFT)

    def SW02():
        print("SW02 is on")
        button1["fg"] = "green"

    #Sets up initial boot screen
    #Creates Row
    topframe = Frame(root)
    topframe.pack(side = TOP)

    #Creating Buttons First Row
    buttonA = Button(topframe, padx=13, pady = 6, bd=4, text="SP2T",fg = "black", command = SP2T)
    buttonA.pack(side = LEFT)

    buttonB = Button(topframe, padx=12, pady = 6, bd=4, text="SP4T",fg = "black")
    buttonB.pack(side = LEFT)

    buttonC = Button(topframe, padx=12, pady = 6, bd=4, text="SP12T",fg = "black")
    buttonC.pack(side = LEFT)

    buttonD = Button(topframe, padx=12, pady = 6, bd=4, text="QUIT", fg="red",command=frame.quit)
    buttonD.pack(side = LEFT)

    #Kick off event loop
    root.mainloop()
    return

run_GUI()

我收到了错误:

Traceback (most recent call last):

  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__

   return self.func(*args)

   File "C:\Python34\gui_attempt.py", line 25, in SW02

   button1["fg"] = "green"

NameError: name 'button1' is not defined

更新版本,希望能够展示整个图片

这个程序还有更多内容,这就是为什么它说第60行,但这是问题所在。我按下时会尝试更改按钮的颜色。

2 个答案:

答案 0 :(得分:2)

最简单的解决方法是将global button添加到需要访问该对象的两个函数的开头:

def SP2T():
    global button1
    ...

def SW02():
    global button1
    ...

但是,使用global通常是一个不好的信号 - 相反,您可以采用基于类的方法,如下例所示:

import tkinter as tk

class MyGui(tk.Tk):

    def __init__(self):
        super(MyGui, self).__init__()
        self.create_widgets()


    def create_widgets(self):
        self.frame = tk.Frame(self)
        self.frame.pack()
        self.buttonA = tk.Button(self.frame, padx=13, pady=6, bd=4, text="SP2T",
                                 fg="black", command=self.SP2T)
        self.buttonA.pack(side=tk.LEFT)

    def SW02(self):
        print("SW02 is on")
        self.button1["fg"] = "green"

    def SP2T(self):
        self.button1 = tk.Button(self.frame, padx=13, pady=6, bd=4, text="SW02",
                                 fg="black", command=self.SW02)
        self.button1.pack(side=tk.LEFT)

if __name__ == "__main__":
    root = MyGui()
    root.mainloop()

答案 1 :(得分:0)

您正在处理scope问题。函数SW02()中发生异常,因为您正在尝试修改不在函数范围内的对象。您需要将其传入。将SW02()更改为以下内容:

def SW02(button1):
    print("SW02 is on")
    button1["fg"] = "green"

由于您将该功能用作命令的操作,因此您可以选择实际传递该功能。

使用lambda:

button1 = Button(frameSP2T, padx=13, pady = 6, bd=4, text="SW02",fg = "black", command = lambda: SW02(button1))

使用functools.partial:

from functools import partial
# other stuff
_SW02 = partial(SW02, button1)
button = Button(frameSP2T, padx=13, pady = 6, bd=4, text="SW02",fg="black", command=_SW02)

或者,正如jonrsharpe的回答所建议的那样,您可以将整个GUI函数重构为一个类,并让所有小部件都是类成员,然后将您的小部件操作与类方法而不是普通函数相关联。这样,修改GUI组件的所有函数都可以访问它们。