猿问

如何在 tkinter python 中动态添加多行文本?

我创建了一个聊天应用程序,在其中我使用 ListBox 来显示聊天记录。它看起来不错,直到我进入一个超出屏幕的长刺。有没有办法打破字符串并显示在新行或任何其他方式来显示完整的字符串。我是 Tkinter 的新手,我不知道有很多小部件可用。


这是我的示例代码


from tkinter import *


class Actions:

    def chatUpdate(chat):

        chatlist.insert(Actions.chatLast,chat)

        Actions.chatLast=Actions.chatLast+1

        chatlist.pack( side=LEFT, fill=BOTH)

        chatBox.config(command=chatlist.yview)



def callUpdater():

    txt=textBox.get()

    text_text.set("")

    Actions.chatUpdate(txt)



root = Tk()

root.title("Chat App")

frame1 = Frame(root, bd=4)

frame1.pack(side=TOP)


frame2 = Frame(root, bd=4)

frame2.pack(side=TOP)


frame3 = Frame(root, bd=4)

frame3.pack(side=TOP)


# chat box

chatBox = Scrollbar(frame1)

chatBox.pack(side=RIGHT, fill=Y)

chatlist = Listbox(frame1, yscrollcommand = chatBox.set, width=50)

Actions.chatLast=0

Actions.chatUpdate("                        ")


# text box

textView = Label(frame2, text="Input: ")

textView.pack(side=LEFT)

text_text = StringVar()

textBox = Entry(frame2, textvariable=text_text, bd=0, width=40, bg="pink")

textBox.pack(side=RIGHT)


# send button

button = Button(frame3, text="Send", fg="black", command=callUpdater)

button.pack(side=TOP)

root.mainloop()



暮色呼如
浏览 400回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答