python图形用户接口实例详解
时间:2020-12-21 17:30:21|栏目:Python代码|点击: 次
本文实例为大家分享了python图形用户接口实例的具体代码,供大家参考,具体内容如下
运用tkinter图形库,模拟聊天应用界面,实现信息发送.
from tkinter import * import time def main(): #发送消息 def sendMsg(): strMsg = '我:'+ time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+'\n' txtMsglist.insert(END,strMsg,'greencolor') txtMsglist.insert(END,txtMsg.get('0.0', END)) txtMsg.delete('0.0',END) #键盘发送消息 def senMsgKeyboard(event): if event.keysym == "Up": sendMsg() #取消发送 def cancelMsg(): txtMsg.delete('0.0', END) #窗口定义 t = Tk() t.title("聊天窗口") # 页面布局 # 创建frame容器 frmLT = Frame(width=500, height=320, bg='white') frmLC = Frame(width=500, height=150, bg='white') frmLB = Frame(width=500, height=30) frmRT = Frame(width=200,height=500) # 创建控件 txtMsglist = Text(frmLT) txtMsglist.tag_config('greencolor', foreground='#008C00') txtMsg = Text(frmLC) # 键盘发送消息 txtMsg.bind("<KeyPress-Up>", senMsgKeyboard) # 按钮发送消息 btnSend = Button(frmLB, text='发送', width=8, command=sendMsg) btnCancel = Button(frmLB, text='取消', width=8, command=cancelMsg) imageLink = PhotoImage(file = "test.gif") lblImage = Label(frmRT, image=imageLink) #控件布置到页面 frmLT.grid(row=0, column=0, columnspan=2, padx=1, pady=3) frmLC.grid(row=1, column=0, columnspan=2, padx=1, pady=3) frmLB.grid(row=2, column=0, columnspan=2) frmRT.grid(row=0, column=2, rowspan=3, padx=2, pady=3) # 固定大小 frmLT.grid_propagate(0) frmLC.grid_propagate(0) frmLB.grid_propagate(0) frmRT.grid_propagate(0) btnSend.grid(row=2, column=0) btnCancel.grid(row=2, column=1) lblImage.grid() txtMsglist.grid() txtMsg.grid() # 主事件循环 t.mainloop() if __name__ == '__main__': main()
界面效果如下:
上一篇:Python面向对象程序设计之继承、多态原理与用法详解
栏 目:Python代码
本文标题:python图形用户接口实例详解
本文地址:http://www.codeinn.net/misctech/34878.html