码迷,mamicode.com
首页 > 编程语言 > 详细

Python入门案例之Hello

时间:2015-04-13 16:44:08      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:tkinter   python   

from Tkinter import *
class Application(Frame):
    def say_hi(self):
        print ‘hello‘

    def createWiegets(self):
        self.QUIT = Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["fg"] = "red"
        self.QUIT["command"] = self.quit

        self.QUIT.pack({"side":"left"})
        self.hi_there = Button(self)
        self.hi_there["text"] = "hello"
        self.hi_there["command"] = self.say_hi

        self.hi_there.pack({"side":"left"})

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.pack()
        self.createWiegets()

root = Tk()
app = Application(master = root)
app.mainloop()
root.destroy()

这段代码说明了如何使用Python来创建一个图形界面。使用的是python中的Tkinter这个图形界面库。

  • 导入相应的模块:from Tkinter import *
  • 定义一个类,继承Frame
class Application(Frame):
    def say_hi(self):
        print ‘hello‘

    def createWiegets(self):
        self.QUIT = Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["fg"] = "red"
        self.QUIT["command"] = self.quit

        self.QUIT.pack({"side":"left"})
        self.hi_there = Button(self)
        self.hi_there["text"] = "hello"
        self.hi_there["command"] = self.say_hi

        self.hi_there.pack({"side":"left"})

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.pack()
        self.createWiegets()
  • 定义控件
def createWiegets(self):
        self.QUIT = Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["fg"] = "red"
        self.QUIT["command"] = self.quit

        self.QUIT.pack({"side":"left"})
        self.hi_there = Button(self)
        self.hi_there["text"] = "hello"
        self.hi_there["command"] = self.say_hi

        self.hi_there.pack({"side":"left"})
  • 创建对象并使用
root = Tk()
app = Application(master = root)
app.mainloop()
root.destroy()

在上面的代码片段中,root是代表的是窗口的容器,我们创建Application对象,并且指定了它的窗口容器,开启消息队列循环,最后销毁窗口。

Python入门案例之Hello

标签:tkinter   python   

原文地址:http://blog.csdn.net/github_20066005/article/details/45026355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!