当前位置: 移动技术网 > IT编程>脚本编程>Python > Python Tkinter 简单使用

Python Tkinter 简单使用

2019年01月20日  | 移动技术网IT编程  | 我要评论

张跃飞,comexception,srooysrooy

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网