初级python代码编程学习----简单的图形化聊天工具

创建一个图形化的聊天工具通常需要使用编程语言和图形用户界面库。以下是一个使用Python和Tkinter库创建的基本图形化聊天工具的代码示例:

代码

import tkinter as tk
from tkinter import scrolledtext

# 创建主窗口
root = tk.Tk()
root.title("图形化聊天工具")

# 设置窗口大小
root.geometry("400x500")

# 创建滚动文本框
scroll = scrolledtext.ScrolledText(root, state='disabled', height=15)
scroll.pack(side='left', fill='both', expand=True)

# 创建消息输入框
entry = tk.Entry(root, width=35)
entry.pack(side='bottom', fill='x')

# 创建发送按钮
send = tk.Button(root, text="发送", command=lambda: send_message())
send.pack(side='bottom', fill='x')

# 发送消息的函数
def send_message():
message = entry.get()
if message:
scroll.configure(state='normal')
scroll.insert(tk.END, f"您: {message}\n")
scroll.configure(state='disabled')
scroll.see(tk.END)
entry.delete(0, tk.END)

# 运行主循环
root.mainloop()

复制代码
import tkinter as tk
from tkinter import scrolledtext

# 创建主窗口
root = tk.Tk()
root.title("图形化聊天工具")

# 设置窗口大小
root.geometry("400x500")

# 创建滚动文本框
scroll = scrolledtext.ScrolledText(root, state='disabled', height=15)
scroll.pack(side='left', fill='both', expand=True)

# 创建消息输入框
entry = tk.Entry(root, width=35)
entry.pack(side='bottom', fill='x')

# 创建发送按钮
send = tk.Button(root, text="发送", command=lambda: send_message())
send.pack(side='bottom', fill='x')

# 发送消息的函数
def send_message():
    message = entry.get()
    if message:
        scroll.configure(state='normal')
        scroll.insert(tk.END, f"您: {message}\n")
        scroll.configure(state='disabled')
        scroll.see(tk.END)
        entry.delete(0, tk.END)

# 运行主循环
root.mainloop()

保存为123.py

可以再装好python环境下运行

Python 123.py

效果

这段代码将创建一个包含滚动文本框、消息输入框和发送按钮的简单聊天界面。用户可以在输入框中输入消息,然后点击发送按钮将消息显示在滚动文本框中。

要运行此代码,您需要安装Python和Tkinter库。在大多数Python安装中,Tkinter是默认安装的,所以您可以直接运行此代码。如果您使用的是Anaconda,Tkinter也应该已经安装好了

相关推荐
头疼的程序员2 分钟前
计算机网络:自顶向下方法(第七版)第三章 学习分享(二)
网络·学习·计算机网络
星期五不见面6 分钟前
AI学习(三)openclow启动(2)2026/03/05
学习
weixin_4434785121 分钟前
flutter组件学习之Flex / Expanded弹性布局组件
javascript·学习·flutter
im_AMBER28 分钟前
Leetcode 136 最小栈 | 逆波兰表达式求值
数据结构·学习·算法·leetcode·
Xzq21050935 分钟前
网络基础学习(一)
网络·学习
Fuliy9640 分钟前
第三阶段:进化与群体智能 (Evolutionary & Swarm Intelligence)
人工智能·笔记·python·学习·算法
ejinxian40 分钟前
Go语言完整学习规划(2026版)- Part 1
学习·go
小陈phd43 分钟前
多模态大模型学习笔记(十六)——Transformer 学习之 Decoder Only
人工智能·笔记·深度学习·学习·自然语言处理·transformer
艾莉丝努力练剑1 小时前
【QT】常用控件(一):初识控件,熟悉QWidget
android·linux·数据库·qt·学习·mysql·qt5
jgyzl1 小时前
2026.3.10 Apache POI的学习及思考
学习·apache