【问题标题】:How to make entry texts to set fit to the entry box using Tkinter, Python?如何使用 Tkinter、Python 使输入文本设置为适合输入框?
【发布时间】:2020-04-24 18:46:17
【问题描述】:

我一直在寻求帮助,但找不到任何我需要的东西.. 我有一些看起来像这样的东西:

因此,当用户输入配料时,它会在线为您提供食谱。在这里,我想修复文本框,因为它只显示一行。当我打印输出时,它包含一个新行并且看起来更好但是一旦我将它插入到输入框中,它会忽略所有新行和空格。 这是我的接口代码:

# Driver code
if __name__ == "__main__":
    # create a GUI window
    master = tk.Tk()

    s = tk.StringVar()

    field1 = tk.Entry(master, textvariable=s)
    field1.config({"background": "LightBlue1", "disabledbackground": "LightSkyBlue1"})
    field2 = tk.Entry(master)
    field2.config({"background": "Ivory", "disabledbackground": "Ivory"})

    field1.place(x=20, y=20, width=660, height=40)
    field2.place(x=20, y=80, width=660, height=500)

    field1.configure(state='disabled')
    field2.configure(state='disabled')

    # set font
    myFont = font.Font(family='Verdana', size=9, weight='bold')

    # set text message for cavas
    text = "Welcome to Recipe Finder!\n" \
           + "Instruction: Click the colored button,\n" \
           + "Type either sentence or words\n" \
           + "into the light blue box."

    canvas = tk.Canvas(master, width=250, height=100, bg='DodgerBlue4')
    canvas.pack()
    canvas.place(x=700, y=40)
    canvas.create_text(125, 50, fill="white", font=myFont, text=text)

    # set the background colour of GUI window
    master.configure(background="lemon chiffon")

    # set the title of GUI window
    master.title("Recipe Finder")

    # set the configuration of GUI window
    master.geometry("970x600")

    # Buttons
    button1 = tk.Button(master, text=' Click for some food jokes! ', fg='black', bg='salmon',
                        command=foodJokes, height=5, width=30)
    button1.place(x=700, y=200)
    button1['font'] = myFont

    button2 = tk.Button(master, text=' Type the ingredients you have! ', fg='black', bg='orange',
                        command=askForIngredients, height=5, width=30)
    button2.place(x=700, y=300)
    button2['font'] = myFont

    Clear = tk.Button(master, text=' CLEAR ', fg='black', bg='white',
                      command=clear, height=5, width=30)
    Clear.place(x=700, y=400)
    Clear['font'] = myFont

    Enter = tk.Button(master, text=' ENTER ', fg='black', bg='white',
                      command=enter, height=5, width=30)
    Enter.place(x=700, y=500)
    Enter['font'] = myFont

虽然有人告诉我使用“grid”更简单更好,但我发现“place”对我来说效果更好......而且我只看到使用 grid 和 pack 的帖子,我不想改变我的所有此时的设置。有没有一种简单的方法可以将输入文本更改为在框内换行?

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您将不得不使用文本小部件而不是条目。条目小部件不允许文本换行,而文本小部件则允许

    【讨论】:

    • 我可以在使用地点时使用文本小部件吗?
    • 是的,您可以使用所有小部件,无论您使用的是 place、grid 还是 pack。我真的建议使用网格。它将使您的代码更易于管理
    • 啊,是的,有人告诉我....当我尝试将它们全部更改为网格时,我遇到了很多问题,所以我就留在原地。但是从下一个项目开始,我将尝试使用网格代替!谢谢!我使用了文本小部件,现在看起来好多了!! :-D
    【解决方案2】:

    使用grid

    • 画出您希望用户界面外观的草图(可以在纸上)。
    • 绘制水平线和垂直线(在下面的示例中为红色)在任何地方小部件之间存在分隔。

    在您的示例中,如下所示:

    在这种情况下,所有小部件都跨越一个列。两个小部件(左下和右上)跨越多行。当grid-ing 时,您将使用rowspan 参数指示小部件跨越多少行。

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 2015-02-11
      相关资源
      最近更新 更多