hello_gui.py 700 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from tkinter import *
  4. import tkinter.messagebox as messagebox
  5. class Application(Frame):
  6. def __init__(self, master=None):
  7. Frame.__init__(self, master)
  8. self.pack()
  9. self.createWidgets()
  10. def createWidgets(self):
  11. self.nameInput = Entry(self)
  12. self.nameInput.pack()
  13. self.alertButton = Button(self, text='Hello', command=self.hello)
  14. self.alertButton.pack()
  15. def hello(self):
  16. name = self.nameInput.get() or 'world'
  17. messagebox.showinfo('Message', 'Hello, %s' % name)
  18. app = Application()
  19. # 设置窗口标题:
  20. app.master.title('Hello World')
  21. # 主消息循环:
  22. app.mainloop()