zoukankan      html  css  js  c++  java
  • Python下用Tkinter进行GUI编程

    Python可用的GUI编程的包很多,Tkinter也是其中一个半标准的工具包。

        作为一个老牌的Python GUI工具包(皮皮书屋里找了本书,竟然是2001年的),它由Tk GUI包装而来。在Windows版里面已经包括了,不用单独下载。

        用Tkinter实现一个简单的GUI程序,单击click按钮时会在终端打印出’hello world’:

    复制代码
    __author__ = 'fyby'
    from Tkinter import *   #引入Tkinter工具包
    def hello():
        print('hello world!')
    
    win = Tk()  #定义一个窗体
    win.title('Hello World')    #定义窗体标题
    win.geometry('400x200')     #定义窗体的大小,是400X200像素
    
    btn = Button(win, text='Click me', command=hello)
    #注意这个地方,不要写成hello(),如果是hello()的话,
    #会在mainloop中调用hello函数,
    # 而不是单击button按钮时出发事件
    btn.pack(expand=YES, fill=BOTH) #将按钮pack,充满整个窗体(只有pack的组件实例才能显示)
    
    mainloop() #进入主循环,程序运行
    复制代码

    image

        当我们写一个较大的程序时,最好将代码分成一个或者是几个类,再看一下Hello World例子

    复制代码
    #-*- encoding=UTF-8 -*-
    __author__ = 'fyby'
    from Tkinter import *
    class App:
        def __init__(self, master):
            #构造函数里传入一个父组件(master),创建一个Frame组件并显示
            frame = Frame(master)
            frame.pack()
            #创建两个button,并作为frame的一部分
            self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
            self.button.pack(side=LEFT) #此处side为LEFT表示将其放置 到frame剩余空间的最左方
            self.hi_there = Button(frame, text="Hello", command=self.say_hi)
            self.hi_there.pack(side=LEFT)
    
        def say_hi(self):
            print "hi there, this is a class example!"
    
    win = Tk()
    app = App(win)
    win.mainloop()
    复制代码

    image

        看完了上面两个无聊的Hello World例子,再来看一个稍微Perfect点的东西吧。Menu组件,自己画一个像样点的程序外壳。

    复制代码
    #-*- encoding=UTF-8 -*-
    __author__ = 'fyby'
    from Tkinter import *
    
    root = Tk()
    
    def hello():
        print('hello')
    
    # 创建一个导航菜单
    menubar = Menu(root)
    menubar.add_command(label="Hello!", command=hello)
    menubar.add_command(label="Quit!",command=root.quit)
    
    root.config(menu=menubar)
    
    mainloop()
    复制代码

    image

            这个程序还是有点无趣,因为我们只是创建了一个顶级的导航菜单,点击后只是在终端中输出hello而已,下面来创建一个下拉菜单,这样才像一个正儿八经的应用大笑在下面的这个例子中,会创建三个顶级菜单,每个顶级菜单中都有下拉菜单(用add_command方法创建,最后用add_cascade方法加入到上级菜单中去),为每个下拉选项都绑定一个hello函数,在终端中打印出hello.

            root.quit是退出这个Tk的实例。

    复制代码
    #-*- encoding=UTF-8 -*-
    __author__ = 'fyby'
    from Tkinter import *
    root = Tk()
    
    def hello():
        print('hello')
    
    def about():
        print('我是开发者')
    
    menubar = Menu(root)
    
    #创建下拉菜单File,然后将其加入到顶级的菜单栏中
    filemenu = Menu(menubar,tearoff=0)
    filemenu.add_command(label="Open", command=hello)
    filemenu.add_command(label="Save", command=hello)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=root.quit)
    menubar.add_cascade(label="File", menu=filemenu)
    
    #创建另一个下拉菜单Edit
    editmenu = Menu(menubar, tearoff=0)
    editmenu.add_command(label="Cut", command=hello)
    editmenu.add_command(label="Copy", command=hello)
    editmenu.add_command(label="Paste", command=hello)
    menubar.add_cascade(label="Edit",menu=editmenu)
    #创建下拉菜单Help
    helpmenu = Menu(menubar, tearoff=0)
    helpmenu.add_command(label="About", command=about)
    menubar.add_cascade(label="Help", menu=helpmenu)
    
    #显示菜单
    root.config(menu=menubar)
    
    mainloop()
    复制代码
      

    image

      写了这一些,差不多对Tkinter有了一个大体的印象了。在Python中用Tkinter绘制GUI界面还是蛮简单的。再把上面的例子扩展一下,和Label标签结合,当单击about的时候,在窗体上打印About的内容,而不是在终端输出。将about函数稍微修改一下。单击about以后将会调用about函数渲染frame绘制一个标签并显示其内容。
    def about():
        w = Label(root,text="开发者感谢名单\nfuyunbiyi\nfyby尚未出现的女朋友\nhttp://www.programup.com网站")
        w.pack(side=TOP)

    image

            关于Tkinter更多的内容,参考http://www.programup.com/wiki/beginning_tkinter/,例子原型主要来自于该网站,还有一本书(就是文章开头提到过的那那本01年的书,http://www.ppurl.com/2012/02/python%E4%B8%8Etkinter%E7%BC%96%E7%A8%8B.html)。还有,国内的书记得《征服Python》中好像也有关于Tkinter的例子,在VeryCD上应该可以找的到大声笑

     
    分类: 读书笔记
  • 相关阅读:
    Codeforces Round #652 (Div. 2) A. FashionabLee(几何)
    轻量应用服务器如何通过修改apache配置文件实现非https的访问多域名到不同子目录程序?
    在Windows环境下使用hexo搭建博客以及部署到gitee / github
    使用WordPress搭建个人手机博客(阿里云)
    访问自己服务器的ip地址
    php环境无法上传文件的解决方法
    SSRF漏洞
    CSRF全家桶(含义,防御,攻击)
    JS实现HTML实体与字符的相互转换
    CentOS系统下载及应用部署
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2548508.html
Copyright © 2011-2022 走看看