Python Tkinter 简单使用

2019-01-21 02:43:58来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

原文链接:https://www.cnblogs.com/xuyiqing/p/10290915.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:20190120-自定义实现split方法

下一篇:数据可视化之 tick_params( 参数 )