2011-12-09 68 views
0

大家好我有這個代碼,似乎沒有工作。我認爲它是因爲我的birthCalc方法試圖訪問init方法中的變量。我的問題是,我應該如何改變這一點,以便我的程序可以訪問這些GUI元素和它們的存儲值。在gui中訪問變量

*請注意,即時通訊嘗試訪問birthCalc方法開始時的變量。

class GUI: 
    def __init__ (self): 
     self.app=Tk() 
     bottomFrame=Frame(self.app) 
     bottomFrame.pack(side="bottom",pady=10) 
     self.app.title("When will you be 1 billion seconds old? ") 
     Label(self.app,text="Please enter your birthday").pack() 
     Label(self.app, text="Month").pack(side="left",padx=2) 
     month=IntVar() 
     month.set(1) 
     months=range(1,13) 
     monthMenu=OptionMenu(self.app,month, *months) 
     monthMenu.pack(side="left") 

     Label(self.app, text="Day").pack(side="left",padx=2) 
     day=IntVar() 
     day.set(1) 
     days=range(1,32) 

     dayMenu=OptionMenu(self.app,day, *days) 
     dayMenu.pack(side="left") 

     Label(self.app, text="Year").pack(side="left",padx=2) 
     year=IntVar() 
     year.set(1901) 
     years=range(1901,2012) 
     yearMenu=OptionMenu(self.app,year, *years).pack(side="left") 

     Label(self.app, text="Hour").pack(side="left",padx=2) 
     hour=IntVar() 
     hour.set(0) 
     hours=range(0,25) 
     hourMenu=OptionMenu(self.app,hour, *hours).pack(side="left") 

     Label(self.app, text="Minute").pack(side="left",padx=2) 
     minute=IntVar() 
     minute.set(0) 
     minutes=range(00,60) 
     minuteMenu=OptionMenu(self.app,minute, *minutes).pack(side="left") 

     Label(self.app, text="Second").pack(side="left",padx=2) 
     second=IntVar() 
     second.set(0) 
     seconds=range(0,60) 
     secondMenu=OptionMenu(self.app,second, *seconds).pack(side="left") 

     Button(bottomFrame,text="Calculate", command=self.birthCalc).pack() 
     textVar=StringVar() 
     textVar.set("You will turn 1 billion seconds old on: ") 
     Label(bottomFrame,text=textVar.get()).pack(side="bottom",pady=5) 


    def birthCalc(self): 
     YEARSEC=31104000 #Values used for converting specified unit into seconds 
     MONTHSEC=2592000 #Ex. There are 60 seconds in a minute 
     DAYSEC=86400 
     HOURSEC=360 
     MINUTESEC=60 

     year=self.(yearMenu).get() #Get input 
     month=self.monthMenu.get()-1  
     day=self.dayMenu.get()-1   #Note that 1 is subtracted because months/days start at 01 (Not 00 as python assumes) 
     hour=self.hourMenu.get() 
     minute=self.minuteMenu.get() 
     second=self.secondMenu.get() 

回答

2

__init__功能,您應該添加元素融入班級爲atributes。 也就是說將monthMenu=OptionMenu(self.app,month, *months)分成self.monthMenu=OptionMenu(self.app,month, *months)