python基础-简单的GUI界面

 

采用tkinter实现了几个简单的GUI界面

调用tkinter的方式非常简单,只需要如下几行代码

1 import Tkinter
2 top = Tkinter.Tk()
3 # Code to add widgets will go here...
4 top.mainloop()


使用Button

        import Tkinter
        import tkMessageBox
        top = Tkinter.Tk()
        
        #add a function
        def hello():
            tkMessageBox.showinfo("title","hello python from s")
            
        #Add a Button
        ok = Tkinter.Button(top,bg='red',text='hello python',command=hello);
        
        #Define the layout and show it
        ok.pack();
        
        top.mainloop()


几种布局方式

 目前看来pack的Bottom和Right都不怎么起作用,或许有什么特殊的限制

import Tkinter
import tkMessageBox
from Tkinter import *
top = Tk()

frame=Frame(top)
frame.pack()


bottomframe = Frame(frame)
bottomframe.pack( side = BOTTOM )

ok = Tkinter.Button(bottomframe,bg='red',text='red');
ok.pack(fill='x',side= TOP);

green = Tkinter.Button(bottomframe,bg='green',text='green');
green.pack(side= LEFT);

blue = Tkinter.Button(bottomframe,bg='blue',text='blue');
blue.pack(side= LEFT);

brown = Tkinter.Button(bottomframe,bg='brown',text='brown');
brown.pack(side= BOTTOM );

top.mainloop()


截图如下

place布局

 1         from Tkinter import *
 2         import tkMessageBox
 3         import Tkinter
 4         
 5         
 6         top = Tkinter.Tk()
 7         
 8         def helloCallBack():
 9            tkMessageBox.showinfo( "Hello Python", "Hello World")
10         
11         B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
12         
13         B.pack()
14         B.place(bordermode=OUTSIDE, height=100, width=100,relx=0.1)
15         top.mainloop()

 

 

Grid布局

import Tkinter
import tkMessageBox
from Tkinter import *
top = Tk()

frame=Frame(top)
frame.pack()


bottomframe = Frame(frame)
bottomframe.pack( side = BOTTOM )

ok = Tkinter.Button(bottomframe,bg='red',text='red');
ok.grid(row=1,column=1);

green = Tkinter.Button(bottomframe,bg='green',text='green');
green.grid(row=1,column=2);

blue = Tkinter.Button(bottomframe,bg='blue',text='blue');
blue.grid(row=2,column=1);

brown = Tkinter.Button(bottomframe,bg='brown',text='brown');
brown.grid(row=2,column=2);

top.mainloop()

 

菜单的使用

        from Tkinter import *
        def donothing():
            filewin = Toplevel(root)
            button = Button(filewin, text="Do nothing button")
            button.pack()
            print ''
        root = Tk()
        menubar = Menu(root)
        
        #add fileMane
        filemenu = Menu(menubar, tearoff=0)
        
        filemenu.add_command(label="New", command=donothing)
        filemenu.add_command(label="Open", command=donothing)
        filemenu.add_command(label="Save", command=donothing)
        filemenu.add_command(label="Save as...", command=donothing)
        filemenu.add_command(label="Close", command=donothing)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=root.quit)
        
        menubar.add_cascade(label="File", menu=filemenu)
        
        
        #editmenu
        editmenu = Menu(menubar, tearoff=0)
        
        editmenu.add_command(label="Undo", command=donothing)
        editmenu.add_separator()
        editmenu.add_command(label="Cut", command=donothing)
        editmenu.add_command(label="Copy", command=donothing)
        editmenu.add_command(label="Paste", command=donothing)
        editmenu.add_command(label="Delete", command=donothing)
        editmenu.add_command(label="Select All", command=donothing)
        
        menubar.add_cascade(label="Edit", menu=editmenu)
        
        
        
        root.config(menu=menubar)
        root.mainloop()

 

 

高级应用比如生成表格之类的操作后续再做研究 

总体上使用下来觉得tkinter还是非常简单的,也适合做一些功能的集成。简单好用,python不会是跳过繁琐的技术学习思想的好助手。  但是效率上可能会有一些问题,比如大规模的展示数据的时候

学习过程中可以发现有很多高级应用可以去深入学习,比如pack布局如何做的更好,布局如何和多线程集合,如果自己来扩展一个表格工程,如果和tkinter剥离成一个普通的程序等等。 

同时看了会介绍,说是python在机器学习中地位重要,一门语言看来除了性能,平民程度也是他推广的一个重要前提。很多懂算法的人,可能不会喜欢搞一个大型的java工程,光是搞工程就耗费无数精力

python确实是可以打开工程之外的另外一扇窗户。

 

posted @ 2014-11-01 23:07  shall we  阅读(1086)  评论(0编辑  收藏  举报