Python-tkinter-聊天窗口GUI


  • 環境python3.x 3.x之后tkinter自帶,jupyter notebook/pycharm

常見的聊天窗口

image.png
  • 聊天窗口布局
左上:聊天歷史信息顯示
左中:當前信息編輯區域
左下:按鈕區域
右側:顯示展示區域

 

  • Frame控件

容器區域布局

frmLT,frmLC,frmLB,frmRT

#創建frmLT容器
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)

 

 

  • 控件對象命名規則

“控件類型” + “功能”

frmLT:   frame + LeftTop
txtMsg:  text控件 + 消息
btnSend: button控件 + 發送

 

 
  • 消息處理:
txtMsg = Text(frmLC)
txtMsg.bind("<KeyPress-Up>", sendMsgEvent)
btnSend = Button(frmLB, text = '發送', width = 8, command = sendMsg)
btnCancel =Button(frmLB, text = '取消', width = 8, command = cancelMsg)

 

  • 圖片處理:
imgInfo = PhotoImage(file = "timg-2.gif")
lblImage = Label(frmRT, image = imgInfo)
lblImage.image = imgInfo

 

  • sendNsg()

回調函數,通過函數指針調用的函數

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)

 


  • cancelMsg()
def cancelMsg():#取消信息
        txtMsg.delete('0.0', END)

 


  • sendMsgEvent(event)
def sendMsgEvent(event):#發送消息事件
    if event.keysym =='Up':
        sendMsg()

 


  • grid()界面布局控制
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()

 

  • 主事件循環
app.mainloop()

 

  • 代碼整體:
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 cancelMsg():#取消信息
        txtMsg.delete('0.0', END)

    def sendMsgEvent(event):#發送消息事件
        if event.keysym =='Up':
            sendMsg()
    #創建窗口
    app = Tk()
    app.title('與python聊天')

    #創建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')#創建tag
    txtMsg = Text(frmLC)
    txtMsg.bind("<KeyPress-Up>", sendMsgEvent)
    btnSend = Button(frmLB, text = '發送', width = 8, command = sendMsg)
    btnCancel =Button(frmLB, text = '取消', width = 8, command = cancelMsg)
    imgInfo = PhotoImage(file = "timg-2.gif")
    lblImage = Label(frmRT, image = imgInfo)
    lblImage.image = imgInfo

    #窗口布局
    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()

    #主事件循環
    app.mainloop()

if  __name__ == "__main__":
    main()

 

 
鏈接:https://www.jianshu.com/p/93481b3b4f1e
https://blog.csdn.net/kun_dl/article/details/79500962

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM