Python GUI框架----Tkinter视频教程笔记

Tkinter视频教程笔记

  1. Button控件
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("400x150")

var = tk.StringVar()
l = tk.Label(window, textvariable=var, bg='green',
             font=('Arial', 12), width=15, height=2)
l.pack()

on_hit = False


def hit_me():
    global on_hit

    if on_hit == False:
        on_hit = True
        var.set('you hit me')
    else:
        on_hit = False
        var.set('')


b = tk.Button(window, text="hit me", width=15, height=2, command=hit_me)
b.pack()

window.mainloop()

在这里插入图片描述
2. Entry和Text控件

# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("400x150")

e = tk.Entry(window, show=None)
e.pack()

def insert_point():
    var = e.get()
    t.insert('insert', var)

def insert_end():
    var = e.get()
    # 插入第一行第一列
    t.insert(1.1, var)

b1 = tk.Button(window, text='insert point', width=15, height=2, command=insert_point)
b1.pack()

b2 = tk.Button(window, text='insert end', width=15, height=2, command=insert_end)
b2.pack()

t = tk.Text(window, height=2)
t.pack()

window.mainloop()

在这里插入图片描述
3. Listbox控件

# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("400x150")

var1 = tk.StringVar()
var2 = tk.StringVar()

l = tk.Label(window, bg='yellow', width=4, textvariable=var1)
l.pack()

def print_selection():
    value = lb.get(lb.curselection())
    var1.set(value)

b1 = tk.Button(window, text='print_selection', width=15, height=2, command=print_selection)
b1.pack()

var2.set((11,22,33,44))
lb = tk.Listbox(window, listvariable=var2)
list_items = [1,2,3,4]
for item in list_items:
    lb.insert('end', item)
lb.insert(1, 'first')
lb.insert(2, 'second')
lb.delete(2)
lb.pack()

window.mainloop()

在这里插入图片描述
4. Menu控件

# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("400x150")

l = tk.Label(window, text='', bg='yellow', width=10)
l.pack()

count = 0
def do_job():
    global count
    l.config(text='do ' + str(count))
    count+=1

menubar = tk.Menu(window)
filemenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='NEW', command=do_job)
filemenu.add_command(label='Open', command=do_job)
filemenu.add_command(label='Save', command=do_job)
filemenu.add_separator()
filemenu.add_command(label='Exit', command=window.quit)

editmenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='Edit', menu=editmenu)
editmenu.add_command(label='Cut', command=do_job)
editmenu.add_command(label='Copy', command=do_job)
editmenu.add_command(label='Paste', command=do_job)

submenu = tk.Menu(filemenu)
filemenu.add_cascade(label='Import', menu=submenu, underline=0)
submenu.add_command(label='SUbmenu1', command=do_job)

window.config(menu=menubar)

window.mainloop()

在这里插入图片描述
5. RadioButton控件

# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("400x150")

var1 = tk.StringVar()

l = tk.Label(window, bg='yellow', width=25, text='empty')
l.pack()

def print_selection():
    l.config(text='you have select ' + var1.get())

r1 = tk.Radiobutton(window, text='Option A', variable=var1, value='A', command=print_selection)
r1.pack()
r2 = tk.Radiobutton(window, text='Option B', variable=var1, value='B', command=print_selection)
r2.pack()
r3 = tk.Radiobutton(window, text='Option C', variable=var1, value='C', command=print_selection)
r3.pack()

window.mainloop()

在这里插入图片描述
6. CheckButton控件

# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("400x150")

var1 = tk.StringVar()
var2 = tk.StringVar()

l = tk.Label(window, bg='yellow', width=20)
l.pack()


def print_selection():
    if(var1.get() == '1') & (var2.get() == '0'):
        l.config(text='I love only Python ')
    elif(var1.get() == '0') & (var2.get() == '1'):
        l.config(text='I love only C++ ')
    elif(var1.get() == '0') & (var2.get() == '0'):
        l.config(text='I do not love either ')
    else:
        l.config(text='I love both ')


c1 = tk.Checkbutton(window, text='Python', variable=var1,
                    onvalue=1, offvalue=0, command=print_selection)
c2 = tk.Checkbutton(window, text='C++', variable=var2,
                    onvalue=1, offvalue=0, command=print_selection)
c1.pack()
c2.pack()

window.mainloop()

在这里插入图片描述
7. MessageBox控件
在这里插入图片描述
8. Scale控件

# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("400x150")

l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()

def print_selection(v):
    l.config(text='you have select ' + v)

s = tk.Scale(window, label='try me', from_=5, to=11,
             orient=tk.HORIZONTAL, length=200, showvalue=0, tickinterval=2,
             resolution=0.01, command=print_selection)
s.pack()

window.mainloop()

在这里插入图片描述
9. Frame框架

# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("200x150")

tk.Label(window, text='on the window').pack()

f = tk.Frame(window)
f.pack()
f_left = tk.Frame(f)
f_right = tk.Frame(f)
f_left.pack(side='left')
f_right.pack(side='right')

tk.Label(f_left, text='on the left1').pack()
tk.Label(f_left, text='on the left2').pack()
tk.Label(f_right, text='On the right').pack()

window.mainloop()

在这里插入图片描述
10. 控件位置摆放的三种方式

# Author:blue
import tkinter as tk
from tkinter import messagebox

window = tk.Tk()
window.title("my window")
window.geometry("200x150")

way1 = '''
tk.Label(window, text=1).pack(side='top')
tk.Label(window, text=1).pack(side='bottom')
tk.Label(window, text=1).pack(side='left')
tk.Label(window, text=1).pack(side='right')
'''
way2 = '''
for i in range(4):
    for j in range(3):
        tk.Label(window, text=1).grid(row=i, column=j, ipadx=10, ipady=10)
'''
way3 = '''tk.Label(window, text=1).place(x=10, y=100, anchor='nw')'''

window.mainloop()
  1. Canvas画布
# Author:blue
import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("200x200")

canvas = tk.Canvas(window, bg='blue', height=100, width=200)
image_file = tk.PhotoImage(file='image1.png')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
x0, y0, x1, y1 = 50, 50, 80, 80
line = canvas.create_line(x0, y0, x1, y1)
oval = canvas.create_oval(x0, y0, x1, y1, fill='red')
arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=0, extent=90)
rect = canvas.create_rectangle(100, 30, 120, 50)
canvas.pack()

def moveit():
    canvas.move(rect, 0, 2)

b = tk.Button(window, text='move', command=moveit).pack()

var1 = tk.StringVar()

window.mainloop()

在这里插入图片描述 12. 实战:登陆

# Author:blue
import tkinter as tk
import pickle
from tkinter import messagebox

window = tk.Tk()
window.title('Welcome to LeLe Python')
window.geometry('400x300')

canvas = tk.Canvas(window, height=200, width=500)
image_file = tk.PhotoImage(file='welcome.png')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')

var1 = tk.StringVar()
var2 = tk.StringVar()

tk.Label(window, text='User name: ').place(x=50, y=150)
tk.Label(window, text='User name: ').place(x=50, y=190)

var1.set('example@python.com')
entry1 = tk.Entry(window, textvariable=var1)
entry1.place(x=160, y=150)

entry2 = tk.Entry(window, textvariable=var2, show='*')
entry2.place(x=160, y=190)

def user_login():
    name = var1.get()
    passwprd = var2.get()
    try:
        with open('user_info.pickle', 'rb') as user_file:
            user_info = pickle.load(user_file)
    except FileNotFoundError:
        with open('user_info.pickle', 'wb') as user_file:
            user_info = {'admin': 'admin'}
            pickle.dump(user_info, user_file)

    if name in user_info:
        if passwprd == user_info[name]:
            tk.messagebox.showinfo(title='Welocme', message=' How are you? ' + name)
        else:
            tk.messagebox.showerror(message='Error, your password is wrong, try again')
    else:
        is_sign_up = tk.messagebox.askyesno(title='Welcome',
                                            message='You have not sign up yet. Sign up today?')
        if is_sign_up:
            user_sign_up()


def user_sign_up():

    def sign_to_LeLe_Python():
        np = new_pwd.get()
        npf = confirm_pwd.get()
        nn = new_name.get()

        with open('user_info.pickle', 'rb') as user_file:
            exist_user_info = pickle.load(user_file)
        if np != npf:
            tk.messagebox.showerror(title='Error', message='Password and confirm password must be the same')
        elif nn in exist_user_info:
            tk.messagebox.showerror(title='Error', message='The user has already signed up!')
        else:
            exist_user_info[nn] = np
            with open('user_info.pickle', 'wb') as user_file:
                pickle.dump(exist_user_info, user_file)
            tk.messagebox.showinfo(title='Welcome', message='You have successfully signed up!')
            window_sign_up.destroy()

    # 创建窗口之上的小窗口
    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('Sign up window')

    new_name = tk.StringVar()
    new_name.set('example@python.com')
    tk.Label(window_sign_up, text='User name: ').place(x=10, y=10)
    extry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
    extry_new_name.place(x=150, y=10)

    new_pwd = tk.StringVar()
    tk.Label(window_sign_up, text='Password: ').place(x=10, y=50)
    extry_new_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
    extry_new_pwd.place(x=150, y=50)

    confirm_pwd = tk.StringVar()
    tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90)
    extry_new_pwd = tk.Entry(window_sign_up, textvariable=confirm_pwd, show='*')
    extry_new_pwd.place(x=150, y=90)

    btn_confirm_sign_up = tk.Button(window_sign_up, command=sign_to_LeLe_Python, text='Sign up')
    btn_confirm_sign_up.place(x=150, y=130)


button1 = tk.Button(window, text='Login', command=user_login)
button1.place(x=160, y=230)
button2 = tk.Button(window, text='Sing up', command=user_sign_up)
button2.place(x=250, y=230)

window.mainloop()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值