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()
에 버튼 색상을 변경 그리고 오류 가지고 :파이썬 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)을 말한다 이유는 그래서이 프로그램에 더가있다,하지만 이것은 문제 영역입니다. 눌렀을 때 버튼의 색상을 변경하려고합니다.
tkinter 가져 오기에서 추가 한 후에도 오류를 실제로 재현하는 [최소 예제] (http://stackoverflow.com/help/mcve)를 제공하십시오. *'(하지 말아야 할)'NameError : name 'SW02'is is 정의되지 않음'함수가 * Button * 정의에서 참조 된 이후에 * 정의되어 있기 때문에, 주위를 바꾸면 오류없이 실행됩니다. – jonrsharpe
두 가지 사소한 변경 (추가 된 가져 오기,'Button '을 만들기 전에 이동 된 함수 정의)을 사용하면 게시 한 코드가 저에게 효과적입니다. 따라서 ** 다른 사람들이 당신의 문제가 무엇인지 정확히 알 수 있도록 최소한의 예 **를 제공해주십시오. http://sscce.org를 참조하십시오. – jonrsharpe
죄송합니다. 이것은 내 첫 번째 게시이므로 인내심에 감사드립니다. – lherron8