Python- GUI(Tkinter)

来源:互联网 发布:飞升真元锻体升级数据 编辑:程序博客网 时间:2024/05/21 18:00

基本窗口

import tkinter as tkwindow = tk.Tk()window.title('my window')window.geometry('200x100')# 这里是窗口的内容window.mainloop()

组件

#labell = tk.Label(window,     text='OMG! this is TK!',    # 标签的文字    bg='green',     # 背景颜色    font=('Arial', 12),     # 字体和字体大小    width=15, height=2  # 标签长宽    )l.pack()    # 固定窗口位置
#按钮加点击事件var = tk.StringVar()    # 这时文字变量储存器l = tk.Label(window,    textvariable=var,   # 使用 textvariable 替换 text, 因为这个可以变化    bg='green', font=('Arial', 12), width=15, height=2)l.pack()on_hit = False  # 默认初始状态为 Falsedef hit_me():    global on_hit    if on_hit == False:     # 从 False 状态变成 True 状态        on_hit = True        var.set('you hit me')   # 设置标签的文字为 'you hit me'    else:       # 从 True 状态变成 False 状态        on_hit = False        var.set('') # 设置 文字为空b = tk.Button(window,    text='hit me',      # 显示在按钮上的文字    width=15, height=2,    command=hit_me)     # 点击按钮式执行的命令b.pack()    # 按钮位置
#登录窗口实例import tkinter as tkwindow = tk.Tk()window.title('Welcome to Mofan Python')window.geometry('450x300')# welcome imagecanvas = tk.Canvas(window, height=200, width=500)image_file = tk.PhotoImage(file='welcome.gif')image = canvas.create_image(0,0, anchor='nw', image=image_file)canvas.pack(side='top')# user informationtk.Label(window, text='User name: ').place(x=50, y= 150)tk.Label(window, text='Password: ').place(x=50, y= 190)var_usr_name = tk.StringVar()var_usr_name.set('example@python.com')entry_usr_name = tk.Entry(window, textvariable=var_usr_name)entry_usr_name.place(x=160, y=150)var_usr_pwd = tk.StringVar()entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')entry_usr_pwd.place(x=160, y=190)def usr_login():    passdef usr_sign_up():    pass# login and sign up buttonbtn_login = tk.Button(window, text='Login', command=usr_login)btn_login.place(x=170, y=230)btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)btn_sign_up.place(x=270, y=230)window.mainloop()
原创粉丝点击