Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我创建了一个简单的聊天GUI。当我输入一个大文本时,它会不情愿地进入(或换行)新行_Python_User Interface_Tkinter_Chat - Fatal编程技术网

Python 我创建了一个简单的聊天GUI。当我输入一个大文本时,它会不情愿地进入(或换行)新行

Python 我创建了一个简单的聊天GUI。当我输入一个大文本时,它会不情愿地进入(或换行)新行,python,user-interface,tkinter,chat,Python,User Interface,Tkinter,Chat,我正在尝试创建一个简单的聊天界面,当我输入一个大文本时,例如: Hello there, how are you? I hope you are doing great 显示为: Hello there, how are you? I h ope you are doing great 此问题出现在聊天日志和入口框中。你能帮我修一下吗 代码如下: [from datetime import datetime from tkinter import * def send(event):

我正在尝试创建一个简单的聊天界面,当我输入一个大文本时,例如:

Hello there, how are you? I hope 
you are doing great
显示为:

Hello there, how are you? I h
ope you are doing great
此问题出现在
聊天日志
入口框
中。你能帮我修一下吗

代码如下:

[from datetime import datetime
from tkinter import *

def send(event):
    msg = EntryBox.get("1.0", 'end-1c').strip()
    EntryBox.delete("0.0", END)

    if msg != '':
        ChatLog.config(state=NORMAL)
        ChatLog.insert(END, current_time + " You: " + msg + '\n\n')
        ChatLog.config(foreground="#750216", font=("Verdana", 12))


def send_by_button():
    msg = EntryBox.get("1.0", 'end-1c').strip()
    EntryBox.delete("0.0", END)

    if msg != '':
        ChatLog.config(state=NORMAL)
        ChatLog.insert(END, current_time + " You: " + msg + '\n\n')
        ChatLog.config(foreground="#750216", font=("Verdana", 12))



base = Tk()
base.title("Simple Chat")
base.geometry("400x500")
base.resizable(width=FALSE, height=FALSE)

now = datetime.now()
current_time = now.strftime("%H:%M")

# Create Chat window
ChatLog = Text(base, bd=0, height="8", width="50", font="Arial", )
ChatLog.config(state=DISABLED)

# Bind scrollbar to Chat window
scrollbar = Scrollbar(base, command=ChatLog.yview, cursor="double_arrow")
ChatLog\['yscrollcommand'\] = scrollbar.set

# Create Button to send message
SendButton = Button(base, font=("Verdana", 12, 'bold'), text="SEND", width="12", height=5,
                    bd=0, fg="#750216", bg="#fff5f5", command=send_by_button)

# Create the box to enter message
EntryBox = Text(base, bd=0, fg="#750216", bg="#fff5f5", highlightcolor="#750216", width="29", height="5", font="Arial")

# Place all components on the screen
scrollbar.place(x=376, y=6, height=406)
ChatLog.place(x=6, y=6, height=386, width=370)
EntryBox.place(x=6, y=421, height=70, width=265)
SendButton.place(x=275, y=421, height=70)


# Refresh GUI window every 0.1 seconds, mainly for the "SEND" button.
# If the entry box does not contain text --> 'Send' button is inactive, otherwise it's activated.

def update():
    if (EntryBox.get("1.0", 'end-1c').strip() == ''):
        SendButton\['state'\] = DISABLED
    elif EntryBox.get("1.0", 'end-1c').strip() != '':
        SendButton\['state'\] = ACTIVE
    base.after(100, update)


base.bind('<Return>', send)
update()

base.mainloop()
[从日期时间导入日期时间
从tkinter进口*
def发送(事件):
msg=EntryBox.get(“1.0”,“end-1c”).strip()
EntryBox.delete(“0.0”,结束)
如果消息!='':
ChatLog.config(状态=正常)
插入(结束,当前时间+“您:”+msg+'\n\n')
ChatLog.config(前台=“#750216”,字体=(“Verdana”,12))
def发送方式按钮():
msg=EntryBox.get(“1.0”,“end-1c”).strip()
EntryBox.delete(“0.0”,结束)
如果消息!='':
ChatLog.config(状态=正常)
插入(结束,当前时间+“您:”+msg+'\n\n')
ChatLog.config(前台=“#750216”,字体=(“Verdana”,12))
base=Tk()
基本标题(“简单聊天”)
基础几何(“400x500”)
可调整大小(宽度=假,高度=假)
now=datetime.now()
当前时间=现在。strftime(“%H:%M”)
#创建聊天窗口
ChatLog=Text(基本,bd=0,height=“8”,width=“50”,font=“Arial”,)
ChatLog.config(状态=已禁用)
#将滚动条绑定到聊天窗口
scrollbar=scrollbar(基本,命令=ChatLog.yview,cursor=“双箭头”)
ChatLog\['yscrollcommand'\]=scrollbar.set
#创建按钮发送消息
SendButton=按钮(底部,字体=(“Verdana”,12,“粗体”),text=“SEND”,width=“12”,height=5,
bd=0,fg=“#750216”,bg=“#fff5f5”,命令=send_by_按钮)
#创建用于输入消息的框
EntryBox=Text(基本,bd=0,fg=“#750216”,bg=“#fff5f5”,highlightcolor=“#750216”,width=“29”,height=“5”,font=“Arial”)
#将所有组件放置在屏幕上
滚动条位置(x=376,y=6,高度=406)
聊天记录位置(x=6,y=6,高度=386,宽度=370)
入口盒位置(x=6,y=421,高度=70,宽度=265)
SendButton.place(x=275,y=421,高度=70)
#每0.1秒刷新一次GUI窗口,主要用于“发送”按钮。
#如果输入框不包含文本-->“发送”按钮处于非活动状态,否则它将被激活。
def update():
if(EntryBox.get(“1.0”,“end-1c”).strip()='':
SendButton\['state'\]=已禁用
elif EntryBox.get(“1.0”,“end-1c”).strip()!=“”:
SendButton\['state'\]=活动
基本。之后(100,更新)
base.bind(“”,send)
更新()
base.mainloop()
我附上了一张图片来澄清我的问题:

在创建
ChatLog
EntryBox
时,您需要添加
wrap=“word”

ChatLog=Text(基本,bd=0,height=“8”,width=“50”,font=“Arial”,wrap=“word”)
...
EntryBox=Text(base,bd=0,fg=“#750216”,bg=“#fff5f5”,highlightcolor=“#750216”,
width=“29”,height=“5”,font=“Arial”,wrap=“word”)

非常感谢。我一直在深入寻找一个解决方案,我不认为这是一个如此简单的解决方案哈哈。非常感谢!我可以在EntryBox中放置占位符文本吗?在
条目
小部件中添加占位符没有什么问题,实现逻辑应该与
文本
类似>小部件。