首页 > 解决方案 > 如何在 python 的 Tkinter GUI 中显示完整的文本?

问题描述

我正在创建一个项目,通过按下按钮,来自另一个函数的文本将出现在 GUI 下该按钮下。

文本是保存在列表中的函数结果。

我想在 GUI 窗口中显示全文,如果不能在小窗口中显示,请继续换行以自动适应

到目前为止,我已经尝试在 Label.grid 中显示结果,但我可以粗略地看到我文本的前半句。我还更改了 ipadx 和 ipady 的值,但这不是我想要的,因为它垂直显示所有句子,直到全部显示


    from tkinter import *
    import tkinter as tk
    from tkinter.messagebox import *

    global my_list
    my_list = ["This is the first sentence , ", "This is the second sentence ,", "this is the third sentence ,", "this is the last sentence "]

    def show_answer():
        Ans = my_list
        blank.insert(0, Ans)


    main = Tk()
    main.title('Programm')
    main.geometry('500x500+300+100')

    Button(main, text='Show', command=show_answer).grid(row=30, column=1, sticky=W, pady=4)

    Label(main, text="The text is").grid(row=2)
    blank = Entry(main)
    blank.grid(row=2, column=1, ipadx=50, ipady=50, sticky="NW")

    mainloop()

标签: pythontkinter

解决方案


您应该使用 tkinter.Text()。

例子 :

text_box = tkinter.Text(parent)
text_box.insert(tkinter.END, "some text...")

这将显示文本并在太长时换行。


推荐阅读