简单的一些实例,能够实现一般的功能就够用了
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()
 
 

Python Tkinter 简单使用的更多相关文章

  1. python+tkinter 简单的登录窗口demo

    一个简单的登录窗口布局,可以用于日常快速搭建一个简单的窗口类. from tkinter import * import tkinter.messagebox class LoginUi: def _ ...

  2. Python tkinter 实现简单登陆注册 基于B/S三层体系结构,实现用户身份验证

    Python tkinter 实现简单登陆注册 最终效果 开始界面 ​ 注册 登陆 ​ 源码 login.py # encoding=utf-8 from tkinter import * from ...

  3. Python Tkinter 学习成果:点歌软件music

    笔者工作业余时间也没什么爱好,社交圈子也小,主要娱乐就是背着自己带电瓶的卖唱音响到住地附近找个人多的位置唱唱KtV. 硬件上点歌就用笔记本电脑,歌曲都是网上下载的mkv格式的含有两个音轨的视频.因此点 ...

  4. Python 实现简单的登录注册界面

    Python 实现简单的登录注册界面 注意:编写代码之前需要导入很重要的包 import tkinter as tk import pickle from tkinter import message ...

  5. Python Tkinter 窗口创建与布局

    做界面,首先需要创建一个窗口,Python Tkinter创建窗口很简单:(注意,Tkinter的包名因Python的版本不同存在差异,有两种:Tkinter和tkinter,读者若发现程序不能运行, ...

  6. Python 实现简单的 Web

    简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...

  7. 用 python实现简单EXCEL数据统计

    任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...

  8. python开启简单webserver

    python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...

  9. Python开发简单爬虫 - 慕课网

    课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序   课程目录 第1章 课程介绍 ...

随机推荐

  1. 7B - 今年暑假不AC

    “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了.  ...

  2. openstack快速复制一台云主机系统

    1.先把目标主机创建快照,目标机器会关机 2.创建主机 3.设置网络和ip: 当我ifconfig eth0的时候出现如下错误:eth0: error fetching interface infor ...

  3. Electorn(桌面应用)自动化测试之Java+selenium实战例子

    基于electorn的桌面应用,网上相关资料较少.所有记录一下.使用java+selenium+testng对该类型应用的自动化测试方法. 代码样例 package com.contract.web. ...

  4. JS 变量提升与函数提升

    JS 变量提升与函数提升 JS变量提升 变量提升是指:使用var声明变量时,JS会将变量提升到所处作用域的顶部.举个简单的例子: 示例1 console.log(foo); // undefined ...

  5. 第二阶段第五次spring会议

    昨天我对软件加上了写便签时自动加上时间的功能. 今天我将对初始页面进行加工和修改. 我用两个小动物作为按钮分别进入动物便签界面和植物便签界面,可以让用户自由选择. 明天我将尝试对软件进行添加搜索引擎的 ...

  6. 【Selenium】【BugList5】chrom窗口未关闭,又新开窗口,报错:[8564:8632:0522/111825.341:ERROR:persistent_memory_allocator.cc(845)] Corruption detected in shared-memory segment.

    环境信息:Windows7 64位 + python 3.6.5 + selenium 3.11.0 +pyCharm 解决方法: 执行 driver = webdriver.Chrome()前必须把 ...

  7. C#算法

    递归 任何一个方法既可以调用其他方法又可以调用自己,而当这个方法调用自己时,我们就叫它递归函数或者递归方法! 通常递归有两个特点: 1.递归方法一直会调用自己直到某些条件满足,也就是说一定要有出口; ...

  8. oracle 中如何查看某个表所涉及的存储过程

    SELECT DISTINCT * FROM user_source WHERE TYPE = 'PROCEDURE' AND upper(text) LIKE '%PS_KL_ABS_002_DAT ...

  9. C语言的数据类型的本质和提高学习

    一.数据类型的概念 类型是对数据的抽象 类型是相同的数据有相同的表示形式.存储格式以及相关的操作 程序中使用的数据必定属于某一种数据类型 ​ 1.算术类型: 包括三种类型:整数类型.浮点类型,枚举型. ...

  10. 2019.03.25 NOIP训练 匹配(match)(贪心)

    题意简述: 思路: 直接考虑把人和物品都看成二维平面上面的a,ba,ba,b两类点,然后一个aaa和bbb匹配的条件是xa≤xb&&ya≤ybx_a\le x_b\&\& ...