首页 > 解决方案 > 如何识别较长的用户输入单词/字符串?

问题描述

我正在尝试为我的家庭作业创建一个小程序,到目前为止,我使用 Tkinter 制作了一个 GUI,但我无法创建可以使程序识别长单词的代码。

如何让程序理解和识别长单词/字符串?

到目前为止,这是我的代码:

import tkinter as tk
import tkinter.messagebox as tkm

def show_name():
    string = entry1.get()
    tkm.showinfo('Longer String', string)

window = tk.Tk()
window.geometry('300x300')

label1 = tk.Label(window, text='String 1')
label1.pack()

entry1 = tk.Entry(window)
entry1.pack()

label2 = tk.Label(window, text='string 2')
label2.pack()

entry2 = tk.Entry(window)
entry2.pack()

label3 = tk.Label(window, text='string 3')
label3.pack()

entry3 = tk.Entry(window)
entry3.pack()

button2 = tk.Button(window, text='Identify longer string', command=show_name)
button2.pack()

window.mainloop()

标签: pythonpython-3.xtkintertkinter-entry

解决方案


我认为您正在尝试找到最长的单词。我在你的代码中添加了一些东西

import tkinter as tk
import tkinter.messagebox as tkm

#We get all values and ve sort this values as length and find the largest word.
def show_name(entry1, entry2, entry3):
    result = sorted([entry1, entry2, entry3], key=len)
    tkm.showinfo('Longer String', result[2])

window = tk.Tk()
window.geometry('300x300')

label1 = tk.Label(window, text='String 1')
label1.pack()

entry1 = tk.Entry(window)
entry1.pack()

label2 = tk.Label(window, text='string 2')
label2.pack()

entry2 = tk.Entry(window)
entry2.pack()

label3 = tk.Label(window, text='string 3')
label3.pack()

entry3 = tk.Entry(window)
entry3.pack()


                                                            # Use there lambda because we don't want to start this function untill we click it and we send entry values in this function.
button2 = tk.Button(window, text='Identify longer string', command=lambda: show_name(entry1.get(), entry2.get(), entry3.get()))
button2.pack()



window.mainloop()

推荐阅读