Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python GUI中更改按钮颜色_Python_Button_User Interface_Colors_Background - Fatal编程技术网

在python GUI中更改按钮颜色

在python GUI中更改按钮颜色,python,button,user-interface,colors,background,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 SP2

我得到了一个错误:

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()
更新版,希望能展示整个画面

这个程序有更多的内容,所以它说第60行,但这是问题所在。按下按钮时,我试图更改按钮的颜色。

您正在处理问题。函数
SW02()
中出现异常,因为您试图修改不在函数作用域中的对象。你需要把它传过来。将
SW02()
更改为以下内容:

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
由于您将函数用作命令的操作,因此有几个选项可以实际传递它

使用lambda:

def SW02(button1):
    print("SW02 is on")
    button1["fg"] = "green"
使用functools.partial:

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

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

最简单的修复方法是在需要访问该对象的两个函数的开头添加
全局按钮

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)
但是,使用
global
通常是一个不好的迹象-相反,您可以采用基于类的方法,如下面的简单示例所示:

def SP2T():
    global button1
    ...

def SW02():
    global button1
    ...

请提供一个实际重新创建错误的函数-即使在从tkinter import*添加
后(您应该避免这样做),我得到
名称错误:名称“SW02”未定义
,因为函数是在
按钮
定义中引用后定义的,如果我切换它们,它会正常运行。通过两个小改动(添加导入,在创建
按钮之前移动函数定义),您发布的代码对我有效。所以,请再次提供一个最小的例子,以便其他人能够确切地看到您的问题是什么。另见,对不起。这是我第一次发帖,所以我很感谢大家的耐心。环球没有带来什么不同。对此我很抱歉。我对python也是相当陌生的,所以帖子中的新问题和python中的新问题的结合让我最初的帖子很糟糕。所以:button1=Button(frameSP2T,padx=13,pady=6,bd=4,text=“SW02”,fg=“black”,command=SW02(button1))button1.pack(side=LEFT)def SW02(input#Button):打印(“SW02打开”)按钮n1[“fg”]=“绿色”和def SW02(input#Button)在新的一行上,还有其他东西我的全部代码吗?或者其他导入和def run_GUI():?“#其他东西”是导入和函数定义之间的一切(我不想复制到我的答案中的现有代码)。因为这是Python 3.x,他不需要在这里使用
global
,他可以使用
非本地
并将它们存储在闭包中。从技术上讲,这相当于创建对象。但实际上,正如你所展示的那样,在这样的情况下使用OO要比python好得多。这可能是一个非常愚蠢的问题,但我相信我已经修复了我的代码以拥有一个类,但是现在所有的东西都在一个函数中定义,如果我像你一样设置了它,任何东西都不会运行。底部的if语句是否运行该程序?在定义了所有函数之后,我尝试调用create_widgets(self),但只显示我的GUI框,没有按钮。我将用我现在的问题更新我的问题。我不确定该怎么办,因为这些示例中没有一个是在试图运行的def中包含def的类。除非您建议将其作为单独的py文件并导入,但我不想这样做。@lherron8不要更改问题,它会使答案无效。但是,请注意,我的代码不包括嵌套在实例方法中的函数。您还有许多
namererror
s。
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()