您的位置:首页 > 编程语言 > Python开发

Python- GUI(Tkinter)

2017-08-16 18:11 218 查看

基本窗口

import tkinter as tk

window = tk.Tk()
window.title('my window')
window.geometry('200x100')

# 这里是窗口的内容

window.mainloop()


组件

#label
l = 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  # 默认初始状态为 False
def 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 tk

window = tk.Tk()
window.title('Welcome to Mofan Python')
window.geometry('450x300')

# welcome image
canvas = 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 information
tk.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():
pass
def usr_sign_up():
pass

# login and sign up button
btn_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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tkinter gui