python的tkinter布局之簡單的聊天視窗實現方法

來源:互聯網
上載者:User
本文執行個體展示了一個python的tkinter布局的簡單聊天視窗。分享給大家供大家參考之用。具體方法如下:

該執行個體展示的是一個簡單的聊天視窗,可以實現下方輸入聊天內容,點擊發送,可以增加到上方聊天記錄列表中。現在只是“單機”版。
右側預留了空位可以放點兒其它東西。感興趣的讀者可以進一步做成socket雙方互聊。

以下是功能代碼部分:

from Tkinter import *import datetimeimport timeroot = 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)#建立一個綠色的tagtext_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)#把元素填充進frametext_msglist.grid()text_msg.grid()button_sendmsg.grid(sticky=E)#主事件迴圈root.mainloop()

以下是運行:

希望本文所述對大家的Python程式設計有所協助

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.