2014-12-14 4 views
-1

나는 TkInter를 가지고 놀았지만 종료하는 데 문제가 있습니다.root.after가 전체 오류를 발생합니다.

from __future__ import absolute_import 
from . import BasePlugin 
import os, sys 
import time 
from Tkinter import * 


class dis(BasePlugin): 

def execute(self, msg, unit, address, when, printer, print_copies): 
    mseg = str('%s - %s' % (msg, unit)) 
    root = Tk() 
    text = Text(root, wrap = 'word', bg= 'red', font= 'times 30 bold') 
    text.insert(INSERT, msg) 
    text.pack() 
    root.title('Pager Printer Display') 
    root.after(6000, _close) 
    root.mainloop(0) 


    PLUGIN = dis 

을하지만 지금은 그 후 닫으 분 말할 필요 :

나는 화면에 텍스트를 얻을 수 있습니다.

그래서 피곤 끝에

def _close(): 
     root.destroy() 

를 추가했습니다 시작, 중간하지만

class dis(BasePlugin): 
    def _close(): 
     root.destroy() 

    def execute(self, msg, unit, address, when, printer, print_copies): 
     mseg = str('%s - %s' % (msg, unit)) 
     root = Tk() 
     text = Text(root, wrap = 'word', bg= 'red', font= 'times 30 bold') 
     text.insert(INSERT, msg) 
     text.pack() 
     root.title('Pager Printer Display') 
     root.after(6000, _close) 
     root.mainloop(0) 

PLUGIN = dis 

사람이 도움을 줄 수 예컨대

Global name _close is undefined 

의 오류?

답변

2

root은 로컬 변수로 정의됩니다. 다른 메소드에서 액세스 가능하게 만들려면이를 인스턴스 속성으로 설정해야합니다. 또한 _close을 인스턴스 변수로 선언하는 것을 잊지 마십시오.

class dis(BasePlugin): 

    def _close(self): 
     self.root.destroy() 

    def execute(self, msg, unit, address, when, printer, print_copies): 
     mseg = str('%s - %s' % (msg, unit)) 
     self.root = Tk() 
     text = Text(self.root, wrap = 'word', bg= 'red', font= 'times 30 bold') 
     text.insert(INSERT, msg) 
     text.pack() 
     self.root.title('Pager Printer Display') 
     self.root.after(6000, self._close) 
     self.root.mainloop(0) 

또 다른 간단한 방법은 root.destroy을 콜백으로 전달하는 것입니다.

class dis(BasePlugin): 

    def execute(self, msg, unit, address, when, printer, print_copies): 
     mseg = str('%s - %s' % (msg, unit)) 
     root = Tk() 
     text = Text(root, wrap = 'word', bg= 'red', font= 'times 30 bold') 
     text.insert(INSERT, msg) 
     text.pack() 
     root.title('Pager Printer Display') 
     root.after(6000, root.destroy) 
     root.mainloop(0) 
+0

건배 중 하나는 치료를했습니다. 나는 그것을 시도 했었다는 것을 맹세한다 그러나 나는 그것에 의해 너무 좌절 될 수 있었다 :). – Shaggy89

관련 문제