2012-07-29 2 views
0

저는 python을 처음 사용합니다. 나는 다른 직원 allready의 목록을 수행하는 위젯 내에서 값을 얻기 위해 대화 상자를 열려고합니다.파이썬에서 위젯 버튼으로 대화 상자를 여는 중 오류가 발생했습니다.

그러나 오류가 발생하여 수행 할 작업을 파악할 수 없습니다. 여기

내 코드입니다 :

, 내가 가져 오기 버튼으로 데이터 세트를 가져 오기 실행이라는 버튼을 통해 다른 코드에서 몇 가지 분석을 실행 한 후 일부 그래프를 보여주는거야 당신을 알 수 있습니다 그래서
import Tkinter,Tkconstants,tkFileDialog 
from Tkinter import * 
import csv 
import numpy 
import math 
import numpy.random as nrnd 
import matplotlib.pyplot as plt 
import shutil 
import tkMessageBox 
global filesavepath 
class App: 
    def __init__(self,master): 
     self.mymaster=master 
     frame=Frame(master) 
     frame.pack() 
     self.importbutton=Button(frame,text='Import Data',command=self.importdata) 
     self.importbutton.pack() 
     self.executebutton=Button(frame,text='Execute',command=self.popup) 
     self.executebutton.pack() 
     self.distribution_rep=Button(frame,text='Repeat Purchase Score Distribution',command=self.distrepbutton) 
     self.distribution_rep.pack() 
     self.distribution_churn=Button(frame,text='Churn Probability Distribution',command=self.distchurnbutton) 
     self.distribution_churn.pack() 
     self.exitbutton=Button(frame,text='Exit',command=self.exitapp) 
     self.exitbutton.pack() 
     self.file_opt=options={} 
     options['defaultextension']='' 
     options['filetypes']=[('allfiles','.*'),('textfiles','.txt')] 
     options['initialdir']='C:\\' 
     options['initialfile']='myfile.txt' 
     options['parent']=root 
     options['title']='Thisisatitle' 
    def importdata(self): 
     filename=tkFileDialog.askopenfilename(**self.file_opt) 
     filesavepath="C:/input_full.csv" 
     shutil.copy2(filename,filesavepath) 
     if filename: 
      return open(filename,'r') 

    def popup(self): 
     top = self.top = Tkinter.Toplevel(self) 
     myLabel = Tkinter.Label(top, text='Enter your username below') 
     myLabel.pack() 

     self.myEntryBox = Tkinter.Entry(top) 
     self.myEntryBox.pack() 

     mySubmitButton = Tkinter.Button(top, text='Done', command=self.execbutton) 
     mySubmitButton.pack() 
    def execbutton(self): 
     if self.myEntryBox.get() != "": 
      self.timevalue = self.myEntryBox.get() 
      self.top.destroy() 
     execfile("Repeat Purchase Algo in python v6") 
     tkMessageBox.showinfo("Job Done", "Probability Computation completed")  
    def send(self): 
     global timevalue 
     timevalue=self.myEntryBox.get() 
     self.top.destroy() 
    def distrepbutton(self): 
     plt.hist(prob,bins=10,normed=TRUE) 
     plt.xlabel('Probability') 
     plt.title('Histogram of Repeat Purchase Probability') 
     plt.show() 
    def distchurnbutton(self): 
     plt.hist(churn_prob,bins=10,normed=TRUE) 
     plt.ylabel('Probability') 
     plt.title('Histogram of Churn Probability') 
     plt.show() 
    def exitapp(self): 
     self.mymaster.destroy() 

root=Tk() 
root.title('Repeat Puchase Widget') 
app=App(root) 
root.mainloop() 

.

내가 원했던 것은 값을 입력 할 "실행"버튼을 클릭 할 때 팝업 창을 여는 것이 었습니다. 하지만 다음과 같은 오류가 나타납니다 :

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ 
    return self.func(*args) 
    File "C:/Python27/widget_repeat_purchase_v4", line 42, in popup 
    top = self.top = Tkinter.Toplevel(self) 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 2017, in __init__ 
    BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__ 
    BaseWidget._setup(self, master, cnf) 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup 
    self.tk = master.tk 
AttributeError: App instance has no attribute 'tk' 

나는 무엇을 해야할지 잘 모릅니다. 도와주세요.

+0

솜 많은 수입이 왜 당신이 방황 경우, 내가 여기 –

답변

1

의 당신은 최상위 위젯을 만들 때, 당신은 첫 번째 인수로 self 전달된다. Tkinter는 이것이 부모 위젯 일 것을 요구합니다. 그러나 코드에서 self은 위젯을 나타내지 않습니다. 특정 경우

당신이 self.mymaster보다는 self에 전달하려는 :

top = self.top = Tkinter.Toplevel(self.mymaster) 
+0

를 통해 실행하고있어 두 번째 파이썬 스크립트입니다 유의하시기 바랍니다 Bryan에게 감사드립니다, 그것은 효과가있었습니다 ... 나는 실수를 깨닫지 못했습니다 ... 정말로 감사합니다. –

0

사용 Tkinter.Toplevel() 대신 Tkinter.Toplevel(self)

관련 문제