手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python入门案例之Hello World!

[日期:2015-04-13] 来源:Linux社区  作者:github_20066005 [字体:大 中 小]

Python入门案例之Hello World!

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对象,并且指定了它的窗口容器,开启消息队列循环,最后销毁窗口。

--------------------------------------分割线 --------------------------------------

CentOS上源码安装Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的详细介绍:请点这里
Python 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-04/116140.htm

linux
相关资讯       Hello World  Python入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款