简单的一些实例,能够实现一般的功能就够用了
Tkinter:
创建顶层窗口:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
root.mainloop()
 
Label使用:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
label = Label(root, text="Hello World!")
label.pack()
root.mainloop()
 
加入一些参数:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
label = Label(root, text="Hello World!", height=10, width=30, fg="black", bg="pink")
label.pack()
root.mainloop()
 
Frame:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
for relief in [RAISED, SUNKEN, RIDGE, GROOVE, SOLID]:
    f = Frame(root, borderwidth=2, relief=relief)
    Label(f, text=relief, width=10).pack(side=LEFT)
    f.pack(side=LEFT, padx=5, pady=5)
root.mainloop()
 
Button:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
Button(root, text="禁用", state=DISABLED).pack(side=LEFT)
Button(root, text="取消").pack(side=LEFT)
Button(root, text="确定").pack(side=LEFT)
Button(root, text="退出", command=root.quit).pack(side=RIGHT)
root.mainloop()
 
给按钮加一些参数:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
Button(root, text="禁用", state=DISABLED, height=2, width=10).pack(side=LEFT)
Button(root, text="取消", height=2, width=10, fg="red").pack(side=LEFT)
Button(root, text="确定", height=2, width=10, fg="blue", activebackground="blue", activeforeground="yellow").pack(
    side=LEFT)
Button(root, text="退出", command=root.quit, fg="black", height=2, width=10).pack(side=RIGHT)
root.mainloop()
 
Entry:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
 
f1 = Frame(root)
Label(f1, text="标准输入框:").pack(side=LEFT, padx=5, pady=10)
e1 = StringVar()
Entry(f1, width=50, textvariable=e1).pack(side=LEFT)
e1.set("请输入内容")
f1.pack()
 
f2 = Frame(root)
e2 = StringVar()
Label(f2, text="禁用输入框:").pack(side=LEFT, padx=5, pady=10)
Entry(f2, width=50, textvariable=e2, state=DISABLED).pack(side=LEFT)
e2.set("不可修改内容")
f2.pack()
 
root.mainloop()
 
小案例:摄氏度转为华氏度
# -*- coding: utf-8 -*-
import Tkinter as tk
 
 
def cToFClicked():
    cd = float(entryCd.get())
    labelcToF.config(text="%.2f摄氏度 = %.2f华氏度" % (cd, cd * 1.8 + 32))
 
 
top = tk.Tk()
top.title("摄氏度转华氏度")
labelcToF = tk.Label(top, text="摄氏度转华氏度", height=5, width=30, fg="blue")
labelcToF.pack()
entryCd = tk.Entry(top, text="0")
entryCd.pack()
btnCal = tk.Button(top, text="计算", command=cToFClicked)
btnCal.pack()
 
top.mainloop()
 
RadioButton:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
 
foo = IntVar()
for text, value in [('red', 1), ('greed', 2), ('black', 3), ('blue', 4), ('yellow', 5)]:
    r = Radiobutton(root, text=text, value=value, variable=foo)
    r.pack(anchor=W)
 
foo.set(2)
root.mainloop()
 
CheckButton:
# -*- coding: utf-8 -*-
from Tkinter import *
 
root = Tk()
root.title("顶层窗口")
 
l = [('red', 1), ('green', 2), ('black', 3), ('blue', 4), ('yellow', 5)]
for text, value in l:
    foo = IntVar()
    c = Checkbutton(root, text=text, variable=foo)
    c.pack(anchor=W)
 
root.mainloop()
 
其他的东西比如文本框,滚动条
其实类似,这里就不全部列出来了,其实最常用的也是上面的这些东西
 
下面做一些小案例:
# -*- coding:utf-8 -*-
from Tkinter import *
 
 
class MainWindow:
    def __init__(self):
        self.frame = Tk()
        self.label_name = Label(self.frame, text="name:")
        self.label_age = Label(self.frame, text="age:")
        self.label_sex = Label(self.frame, text="sex:")
        self.text_name = Text(self.frame, height=1, width=30)
        self.text_age = Text(self.frame, height=1, width=30)
        self.text_sex = Text(self.frame, height=1, width=30)
        self.label_name.grid(row=0, column=0)
        self.label_age.grid(row=1, column=0)
        self.label_sex.grid(row=2, column=0)
        self.button_ok = Button(self.frame, text="ok", width=10)
        self.button_cancel = Button(self.frame, text="cancel", width=10)
        self.text_name.grid(row=0, column=1)
        self.text_age.grid(row=1, column=1)
        self.text_sex.grid(row=2, column=1)
        self.button_ok.grid(row=3, column=0)
        self.button_cancel.grid(row=3, column=1)
        self.frame.mainloop()
 
 
frame = MainWindow()
 
最后一个综合案例
计算器:
# -*- coding:utf-8 -*-
from Tkinter import *
 
 
def frame(root, side):
    w = Frame(root)
    w.pack(side=side, expand=YES, fill=BOTH)
    return w
 
 
def button(root, side, text, command=None):
    w = Button(root, text=text, command=command)
    w.pack(side=side, expand=YES, fill=BOTH)
    return w
 
 
class Calculator(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.option_add('*Font', 'Verdana 12 bold')
        self.pack(expand=YES, fill=BOTH)
        self.master.title('Simple Cal')
        self.master.iconname('calc1')
 
        display = StringVar()
        Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
        for key in ('123', '456', '789', '+0.'):
            keyF = frame(self, TOP)
            for char in key:
                button(keyF, LEFT, char, lambda w=display, c=char: w.set(w.get() + c))
        opsF = frame(self, TOP)
 
        for char in '-*/=':
            if char == '=':
                btn = button(opsF, LEFT, char)
                btn.bind('<ButtonRelease-1>', lambda e, s=self, w=display: s.calc(w), '+')
            else:
                btn = button(opsF, LEFT, char, lambda w=display, s='%s' % char: w.set(w.get() + s))
 
        clearF = frame(self, BOTTOM)
        button(clearF, LEFT, 'CLEAR', lambda w=display: w.set(''))
 
    def calc(self, display):
        try:
            display.set(eval(display.get()))
        except:
            display.set('ERROR')
 
 
if __name__ == '__main__':
    Calculator().mainloop()
 
 

最新文章

  1. 《连载 | 物联网框架ServerSuperIO教程》- 7.自控通讯模式开发及注意事项
  2. MyBatis处理一行数据-MyBatis使用sum语句报错-MyBatis字段映射-遁地龙卷风
  3. tab标签切换
  4. 2014 UESTC暑前集训图论专题解题报告
  5. C#环境下,文本框翻屏,怎么一直显示当前插入的内容!!!!!!!!!!!!!!!!
  6. [ActionScript 3.0] AS3调用百度天气预报查询API
  7. 2. ProGit-Git基础
  8. HD1005Number Sequence
  9. Tomcat启用HTTPS(生成证书、配置Tomcatserver)
  10. ubuntu vim之php函数提示
  11. Hacker(十)----常用入侵工具
  12. poj1651 最优矩阵乘法动态规划解题
  13. china-pub
  14. 用VC制作应用程序启动画面
  15. SpringMVC 学习笔记(两) @RequestMapping、@PathVariable和其他注意事项
  16. redhat 安装配置samba实现win共享linux主机目录
  17. javamail邮件发送
  18. AbstractQueuedSynchronizer的简单分析
  19. datetimepicker.js 使用笔记
  20. 通过Jmeter 代理功能获取postman请求

热门文章

  1. lombok的简单介绍
  2. UCloud双11活动 - 新人UCloud代金券最低年100元香港云服务器
  3. HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
  4. docker_sd
  5. flex布局嵌套之高度自适应
  6. Vue-箭头函数
  7. 深入理解java虚拟机(二)-----垃圾回收
  8. 解决更新ssh后在/etc/init.d下无sshd的问题
  9. 微信小程序的自定义插件
  10. Error resolving template [xxx], template might not exist or might not be exist