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确实是可以打开工程之外的另外一扇窗户。

 

转载于:https://www.cnblogs.com/alwaysthinking/p/4067926.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值