[英]Changing Button Color in a python GUI
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行,但这是问题所在。 我试图更改按下按钮时的颜色。
最简单的解决方法是在需要访问该对象的两个函数的开头添加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()
您正在处理范围问题。 在函数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组件的所有功能都可以访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.