Tkinter 8.5 GUI for Python-----01

1小例子A minimal application

# -*- coding: utf-8 -*-

#!/usr/bin/env python                    ##正确安装python后,双击可自动执行

import Tkinter as tk                      ##导入Tkinter包命名为tk

class Application(tk.Frame):                ##自定义类继承自Frame

  def __init__(self,master=None):           ##自定义类的构造函数

    tk.Frame.__init__(self,master)          ##调用父类的构造函数

    self.grid()                           ##确保框架正确的显示在窗体内

    self.createWidgets()                  ##调用自定义方法

  def createWidgets(self):

self.quitButton= tk.Button(self,          ##创建按钮

text='退出',command=self.close)  

    self.quitButton.grid()                  ##将按钮显示到窗体

  def close(self):                         ##获取顶级窗体然后销毁,button的事件

    self.winfo_toplevel().destroy()           ## command=self.quit 解决按钮卡死

app = Application()                        ##创建实例

app.master.title('Sampleapplication')          ##设置窗体的标题

app.mainloop()                           ##启动窗体mainloop,等待键盘和鼠标事件

 

术语

Widget-组件、控件例如:buttons, radiobuttons, text fields, frames, and text labels

Frame-包含组件的矩形区域,a rectangular area that can contain other widgets

child, parent-当创建组件并放入一个frame时,就会创建父子关系

2窗体布局Layout management

Tkinter包含3中布局方式,作者强烈建议使用grid布局,将窗体内部区域分成类似表格的形式,创建组件后只有使用布局方式注册后,才会在窗体中显示出来。

2.1grid()方法参数

Column--控件放置位置的列数,从0开始算起,默认为0;

Clomnspan--设置单元格横向跨越的列数   

in_--重新设置w为窗体w2的子窗体,方法:in_=w2.w2必须是w的父窗体子类;

ipadx--设置控件里面x方向空白区域大小;

ipady--设置控件里面y方向空白区域大小;

padx--设置控件周围x方向空白区域保留大小;

pady--设置控件周围y方向空白区域保留大小;

row--控制放置的行数,从0开始算起,默认为上一个位占领的行数

rowspan--设置单元格纵向跨越的列数rowspan=4, columnspan=5);

sticky--设置对齐方式,默认为居中

sticky=tk.NE (右上角) tk.SE (右下角) tk.SW (左下角) tk.NW (左上角)

sticky=tk.N (上中),tk.E (右中), tk.S (下中), tk.W (左中).

sticky=tk.N+tk.S 居中并垂直拉伸

sticky=tk.E+tk.W 居中并水平拉伸

sticky=tk.N+tk.E+tk.S+tk.W 拉伸填充表格单元

2.2窗体重绘

使用grid布局,使组件填充到整个单元并随窗体的大小变化而重绘

# -*- coding: utf-8 -*-

#!/usr/bin/env python

import Tkinter as tk           

class Application(tk.Frame):

  def __init__(self,master=None):

    tk.Frame.__init__(self,master)

    self.grid(sticky=tk.N+tk.S+tk.E+tk.W)

    self.createWidgets()

  def close(self):

    self.winfo_toplevel().destroy()

  def createWidgets(self):

    top=self.winfo_toplevel()

    top.rowconfigure(0, weight=1)

    top.columnconfigure(0, weight=1)

    self.rowconfigure(0, weight=1)

    self.columnconfigure(0, weight=1)

    self.quit =tk.Button(self, text='Quit', command=self.close)

    self.quit.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)

app = Application()

app.master.title('Sampleapplication')

app.mainloop()

转载于:https://www.cnblogs.com/weiwg/p/8214004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值