python的tkinter布局之简单的聊天窗口实现方法


Posted in Python onSeptember 03, 2014

本文实例展示了一个python的tkinter布局的简单聊天窗口。分享给大家供大家参考之用。具体方法如下:

该实例展示的是一个简单的聊天窗口,可以实现下方输入聊天内容,点击发送,可以增加到上方聊天记录列表中。现在只是“单机”版。
右侧预留了空位可以放点儿其它东西。感兴趣的读者可以进一步做成socket双方互聊。

以下是功能代码部分:

from Tkinter import *
import datetime
import time
root = Tk()
root.title(unicode('与xxx聊天中','eucgb2312_cn'))
#发送按钮事件
def sendmessage():
  #在聊天内容上方加一行 显示发送人及发送时间
  msgcontent = unicode('我:','eucgb2312_cn') + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) + '\n '
  text_msglist.insert(END, msgcontent, 'green')
  text_msglist.insert(END, text_msg.get('0.0', END))
  text_msg.delete('0.0', END)

#创建几个frame作为容器
frame_left_top   = Frame(width=380, height=270, bg='white')
frame_left_center  = Frame(width=380, height=100, bg='white')
frame_left_bottom  = Frame(width=380, height=20)
frame_right     = Frame(width=170, height=400, bg='white')
##创建需要的几个元素
text_msglist    = Text(frame_left_top)
text_msg      = Text(frame_left_center);
button_sendmsg   = Button(frame_left_bottom, text=unicode('发送','eucgb2312_cn'), command=sendmessage)
#创建一个绿色的tag
text_msglist.tag_config('green', foreground='#008B00')
#使用grid设置各个容器位置
frame_left_top.grid(row=0, column=0, padx=2, pady=5)
frame_left_center.grid(row=1, column=0, padx=2, pady=5)
frame_left_bottom.grid(row=2, column=0)
frame_right.grid(row=0, column=1, rowspan=3, padx=4, pady=5)
frame_left_top.grid_propagate(0)
frame_left_center.grid_propagate(0)
frame_left_bottom.grid_propagate(0)
#把元素填充进frame
text_msglist.grid()
text_msg.grid()
button_sendmsg.grid(sticky=E)
#主事件循环
root.mainloop()

以下是运行截图:

python的tkinter布局之简单的聊天窗口实现方法

希望本文所述对大家的Python程序设计有所帮助

Python 相关文章推荐
python计算时间差的方法
May 20 Python
12步教你理解Python装饰器
Feb 25 Python
利用python批量检查网站的可用性
Sep 09 Python
urllib和BeautifulSoup爬取维基百科的词条简单实例
Jan 17 Python
Python OpenCV实现图片上输出中文
Jan 22 Python
Python3 利用requests 库进行post携带账号密码请求数据的方法
Oct 26 Python
Pandas 按索引合并数据集的方法
Nov 15 Python
深入理解Django-Signals信号量
Feb 19 Python
Python中三元表达式的几种写法介绍
Mar 04 Python
python自动化工具之pywinauto实例详解
Aug 26 Python
Python字符串和正则表达式中的反斜杠('\')问题详解
Sep 03 Python
Python谱减法语音降噪实例
Dec 18 Python
python中遍历文件的3个方法
Sep 02 #Python
python中的五种异常处理机制介绍
Sep 02 #Python
python之yield表达式学习
Sep 02 #Python
python中的hashlib和base64加密模块使用实例
Sep 02 #Python
python多线程编程中的join函数使用心得
Sep 02 #Python
python中的函数用法入门教程
Sep 02 #Python
python爬虫入门教程之点点美女图片爬虫代码分享
Sep 02 #Python
You might like
php实现获取文件mime类型的方法
2015/02/11 PHP
php快速查找数据库中恶意代码的方法
2015/04/01 PHP
PHP邮箱验证示例教程
2016/06/01 PHP
Yii2实现让关联字段支持搜索功能的方法
2016/08/10 PHP
php中static和const关键字用法分析
2016/12/07 PHP
javascript+css 网页每次加载不同样式的实现方法
2009/12/27 Javascript
Javascript中3种实现继承的方法和代码实例
2014/08/12 Javascript
利用原生JavaScript获取元素样式只是获取而已
2014/10/08 Javascript
javascript实现3D切换焦点图
2015/10/16 Javascript
jquery $.trim()去除字符串空格的实现方法【附图例】
2016/03/30 Javascript
浅析函数声明和函数表达式——函数声明的声明提前
2016/05/03 Javascript
Angularjs通过指令监听ng-repeat渲染完成后执行脚本的方法
2016/12/31 Javascript
深入理解Promise.all
2018/08/08 Javascript
Vue.js子组件向父组件通信的方法实例代码详解
2018/12/10 Javascript
cordova+vue+webapp使用html5获取地理位置的方法
2019/07/06 Javascript
vue+node 实现视频在线播放的实例代码
2020/10/19 Javascript
python在windows和linux下获得本机本地ip地址方法小结
2015/03/20 Python
用Python编写一个每天都在系统下新建一个文件夹的脚本
2015/05/04 Python
Python实现从URL地址提取文件名的方法
2015/05/15 Python
python函数的5种参数详解
2017/02/24 Python
Python有序查找算法之二分法实例分析
2017/12/11 Python
python做量化投资系列之比特币初始配置
2018/01/23 Python
基于Python开发chrome插件的方法分析
2018/07/07 Python
python使用response.read()接收json数据的实例
2018/12/19 Python
Python时间和字符串转换操作实例分析
2019/03/16 Python
h5页面背景图很长要有滚动条滑动效果的实现
2021/01/27 HTML / CSS
英国第一的市场和亚马逊替代品:OnBuy
2019/03/16 全球购物
植村秀美国官网:Shu Uemura美国
2019/03/19 全球购物
印尼第一大家居、生活和家具电子商务:Ruparupa
2019/11/25 全球购物
好家长事迹材料
2014/01/23 职场文书
减负增效提质方案
2014/05/23 职场文书
高速铁道技术专业求职信
2014/08/09 职场文书
制定企业培训计划的五大要点!
2019/07/10 职场文书
MySQL大小写敏感的注意事项
2021/05/24 MySQL
Grafana可视化监控系统结合SpringBoot使用
2022/04/19 Redis
Python 统计序列中元素的出现频度
2022/04/26 Python