python - 如何识别较长的用户输入单词/字符串?
问题描述
我正在尝试为我的家庭作业创建一个小程序,到目前为止,我使用 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()
解决方案
我认为您正在尝试找到最长的单词。我在你的代码中添加了一些东西
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()
推荐阅读
- android - 深层链接网址可以播放商店/应用商店,但如果安装则不打开应用
- c# - String.Join - 参数和直接值的不同行为
- java - 为什么我要使用 Lombok-Annotation @NonNull?
- java - 是否可以为多模块项目创建 1 个 Spring 组件索引?
- janusgraph - 通过 Gremlin 集成 Graphexp 和 JanusGraph
- javascript - 在 Bootstrap datetimepicker.js 中禁用切换到年份视图
- linux - 如何从文件中grep一个单词并仅显示带有后缀的单词而不是整行?
- python - 用于将给定日期最多添加 999999999 天的 Python 库
- z3 - 在 Z3 中创建可变宽度蒙版约束
- r - 通过对现有级别进行分组来创建新级别