python 窗口程序模板

python 窗口程序模板,实现窗口展示,按钮设置,文本读取,事件触发

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:hcx
 
import tkinter as tk  # 使用Tkinter前需要先导入
import time
 
# 第1步,实例化object,建立窗口window
window = tk.Tk()
 
# 第2步,给窗口的可视化起名字
window.title('My Window')
 
# 第3步,设定窗口的大小(长 * 宽)
window.geometry('500x300')  # 这里的乘是小x
 
# 第4步,在图形界面上设定输入框控件entry框并放置
e = tk.Entry(window,width=12, show = None)#显示成明文形式
e.place(x=10, y=15)
 
# 第5步,定义两个触发事件时的函数insert_point和insert_end(注意:因为Python的执行顺序是从上往下,所以函数一定要放在按钮的上面)
def insert_point(): # 在鼠标焦点处插入输入内容
    var = e.get()
    t.insert('insert', var)
def insert_end():   # 在文本框内容最后接着插入输入内容
    var = e.get()
    t.insert('end', var)
def insert_time():   # 在文本框内容最后接着插入输入内容
    
    t.insert('end', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+'\n' )
 
# 第6步,创建并放置两个按钮分别触发两种情况
b1 = tk.Button(window, text='insert point', width=10, height=2, command=insert_point)
b1.place(x=10, y=100)
b2 = tk.Button(window, text='insert end', width=10,   height=2, command=insert_end)
b2.place(x=10, y=150)
b2 = tk.Button(window, text='insert time', width=10,   height=2, command=insert_time)
b2.place(x=10, y=200)
 
# 第7步,创建并放置一个多行文本框text用以显示,指定height=3为文本框是三个字符高度
t = tk.Text(window, width=50,height=20)
t.place(x=120, y=15)
 
# 第8步,主窗口循环显示
window.mainloop()
  • 5
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值