2017-09-12 2 views
-2

GUI를 사용하여 LED를 켜고 끄려고합니다.Toggle LED Python 3.x tkinter

코드를 실행할 때 제목에 "tk"라고 말하는 빈 상자가 나타납니다.

import tkinter as tk 

class Application(tk.Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 
     self.pack() 
     Label(frame, text='Turn LED ON').grid(row=0, column=0) 
     Label(frame, text='Turn LED OFF').grid(row=1, column=0) 

     self.button = Button(frame, text='LED 0 ON', command=self.convert0) 
     self.button.grid(row=2, columnspan=2) 

    def convert0(self, tog=[0]): 
     tog[0] = not tog[0] 
     if tog[0]: 
      self.button.config(text='LED 0 OFF') 
     else: 
      self.button.config(text='LED 0 ON') 

root = tk.Tk() 
app = Application(master=root) 
app.mainloop() 

답변

0

코드에 몇 가지 수정 사항이 필요합니다.

NameError: name 'Label' is not defined 

그리고 확실히

, 그것은하지 : 그것을 실행

우선은 그대로 나에게 다음과 같은 오류를했다.

Label(frame, text='Turn LED ON').grid(row=0, column=0) 
Label(frame, text='Turn LED OFF').grid(row=1, column=0) 

로 : 정의는 무엇 는 그래서 그 두 줄을 변경할 수 있습니다, tk.Label입니다 충분히

NameError: name 'frame' is not defined 

그리고 확인 : 이제

tk.Label(frame, text='Turn LED ON').grid(row=0, column=0) 
tk.Label(frame, text='Turn LED OFF').grid(row=1, column=0) 

을, 나는 다음과 같은 오류가 발생 해요 , 그것도 아니다. 아마도 Application 클래스가 tk.Frame 클래스를 확장한다는 사실을 알 수 있습니다. 글쎄, 그건 사실이야,하지만 그게 뭔지 알려주지 frame입니다. 나는 frame은 "인스턴스지만, Frame 인스턴스로 간주됩니다"를 의미한다고 가정합니다. 이 경우 self이면 충분합니다. 실제로 필요한 부분입니다.

tk.Label(frame, text='Turn LED ON').grid(row=0, column=0) 
tk.Label(frame, text='Turn LED OFF').grid(row=1, column=0) 

self.button = Button(frame, text='LED 0 ON', command=self.convert0)  

가 될 :

지금, 나는

NameError: name 'Button' is not defined 

내가 당신이 시작하고 확신 것을 이야기하고 있습니다 그래서 여기에 우리는 다음과 같은 세 가지 라인으로 이동 요점을 이해하십시오. 그럼 Buttontk.Button으로 대체 할 : 여기 당신은 간다

self.button = tk.Button(self, text='LED 0 ON', command=self.convert0) 

을 그리고, 윈도우는 그 텍스트를 클릭하면 변경이 라벨과 하나 개의 멋진 버튼으로 표시됩니다.

+0

단계 수정으로 단계를 제공해 주셔서 감사합니다. – Velkoid