这样做的目的是让一个窗口从用户那里获取一个zipcode并保存。保存邮政编码后,它将打开一个新窗口,使用该邮政编码显示各种天气信息。
class EntryW(tk.Frame):
def __init__( self, master ):
self.master = master
self.frame = tk.Frame(self.master)
self.master.title("Weather by Josh")
self.label = Label(self, text = "Enter Zipcode")
self.label.pack(side = LEFT)
global zipcodeEntryVar
zipcodeEntryVar = tk.StringVar()
self.entry = Entry(self,textvariable=zipcodeEntryVar)
self.entry.pack(side = RIGHT)
self.button1 = Button( self, text = "Submit",
command +self.new_window )
self.button1.pack(side = BOTTOM )
self.frame.pack()
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = WeatherApp(self.newWindow)
class WeatherApp(tk.Frame):
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.quitButton = tk.Button(self.frame, text = 'Quit',
width = 25, command = self.close_windows)
self.quitButton.pack()
self.frame.pack()
def close_windows(self):
self.master.destroy()
def main():
root = tk.Tk()
app = EntryW(root)
root.mainloop()
if __name__ == '__main__':
main()