Python 根据输入创建按钮,获取用户数据

Python 根据输入创建按钮,获取用户数据,python,button,tkinter,Python,Button,Tkinter,相对于Python,noob仍然在学习所有的细节,但我正在学习。我第一次潜入GUI是为了一个我正在做的个人项目。(我是一名语言学研究生,这将极大地提高我的研究能力。)我知道Tkinter和Button类(基本上,它们是存在的),但我需要一些帮助才能开始。我想一旦我知道了这些神奇的词语,我就能够根据我需要的情况来调整它 基本上,我有大约180个单词的样本文本摘录。我想做的是找到一种创建GUI界面的方法,这样180字摘录中的每个单词都会显示为一个单独的按钮,并提示用户(例如)单击动词。单击的值被存储

相对于Python,noob仍然在学习所有的细节,但我正在学习。我第一次潜入GUI是为了一个我正在做的个人项目。(我是一名语言学研究生,这将极大地提高我的研究能力。)我知道Tkinter和Button类(基本上,它们是存在的),但我需要一些帮助才能开始。我想一旦我知道了这些神奇的词语,我就能够根据我需要的情况来调整它

基本上,我有大约180个单词的样本文本摘录。我想做的是找到一种创建GUI界面的方法,这样180字摘录中的每个单词都会显示为一个单独的按钮,并提示用户(例如)单击动词。单击的值被存储,然后我继续下一个问题

我需要知道的是: 如何根据文本内容创建按钮。(我假设每个按钮都需要不同的变量名。) -如果一段摘录的长度与另一段不同,这有关系吗?(我想不会吧。) -如果摘录中有几个相同的词,这有关系吗?(我假设不是,因为你可以使用索引来记住单词clicked在原始摘录中的位置。) 如何根据单击的按钮获取存储的数据。 如何收拾残局,继续我的下一个问题


提前感谢您的帮助。

这是一个小示例和演示--它提供了启动程序所需的一切。请参见代码中的注释:

import tkinter

app = tkinter.Tk()

# Create a set for all clicked buttons (set prevents duplication)
clicked = set()
# Create a tuple of words (your 180 verb goes here)
words = 'hello', 'world', 'foo', 'bar', 'baz', 'egg', 'spam', 'ham'

# Button creator function
def create_buttons( words ):
    # Create a button for each word
    for word in words:
        # Add text and functionality to button and we are using a lambda
        # anonymous function here, but you can create a normal 'def' function
        # and pass it as 'command' argument
        button = tkinter.Button( app,
                                 text=word,
                                 command=lambda w=word: clicked.add(w) )
        # If you have 180 buttons, you should consider using the grid()
        # layout instead of pack() but for simplicity I used this one for demo
        button.pack()

# For demo purpose I binded the space bar, when ever
# you hit it, the app will print you out the 'clicked' set
app.bind('<space>', lambda e: print( *clicked ))

# This call creates the buttons
create_buttons( words )

# Now we enter to event loop -> the program is running
app.mainloop()

导入tkinter
app=tkinter.Tk()
#为所有单击的按钮创建一个集合(集合防止重复)
单击=设置()
#创建一个单词元组(180个动词在这里)
单词='hello'、'world'、'foo'、'bar'、'baz'、'egg'、'spam'、'ham'
#按钮创建器功能
def创建按钮(文字):
#为每个单词创建一个按钮
用文字表示:
#将文本和功能添加到按钮,我们将使用lambda
#匿名函数,但您可以创建一个普通的“def”函数
#并将其作为“command”参数传递
按钮=tkinter.按钮(应用程序,
文本=单词,
command=lambda w=word:clicked.add(w))
如果你有180个按钮,你应该考虑使用GRID()
#布局而不是pack(),但为了简单起见,我在演示中使用了这个布局
button.pack()
#为了演示的目的,我绑定了空格键
#你点击它,应用程序将打印出“点击”集
应用程序绑定(“”,lambda e:打印(*单击))
#此调用创建按钮
创建按钮(单词)
#现在我们进入事件循环->程序正在运行
app.mainloop()

编辑:

以下是不带lambda表达式的代码:

导入tkinter
app=tkinter.Tk()
#为所有单击的按钮创建一个集合(集合防止重复)
单击=设置()
#创建一个单词元组(180个动词在这里)
单词='hello'、'world'、'foo'、'bar'、'baz'、'egg'、'spam'、'ham'
#按下空格键时,此功能将运行
按空格键时显示def(事件):
打印('Clicked words:',*Clicked)
#按钮创建器功能
def创建按钮(文字):
#为每个单词创建一个按钮
用文字表示:
#单击按钮时,此功能将运行
def on_按钮_单击(word=word):
单击。添加(word)
#添加按钮
按钮=tkinter.按钮(应用程序,
文本=单词,
命令=打开按钮(单击)
如果你有180个按钮,你应该考虑使用GRID()
#布局而不是pack(),但为了简单起见,我在演示中使用了这个布局
button.pack()
#绑定函数tp空格键事件
应用程序绑定(“”,在空格键上按)
#此调用创建按钮
创建按钮(单词)
#现在我们进入事件循环->程序正在运行
app.mainloop()

大家好,首先,欢迎来到StackOverflow。不幸的是,你的问题对于这个网站来说有点模糊。如果您可以发布一些您尝试过的代码,其中有一个特定的问题与代码中您不理解或不起作用的内容有关,那么我们可以帮助您。亲爱的@user2974982如果您认为发布的答案解决了您的问题,或者至少是找到您正在寻找的解决方案的有用线索,请毫不犹豫地接受它。如果你不知道如何接受答案,你可以在这里找到更多的信息:OP就是noob。也许你应该将按钮绑定到普通的“def”(正如你所建议的)来显示更多。你可能是对的@furas我也添加了lambda免费版本!
import tkinter

app = tkinter.Tk()

# Create a set for all clicked buttons (set prevents duplication)
clicked = set()
# Create a tuple of words (your 180 verb goes here)
words = 'hello', 'world', 'foo', 'bar', 'baz', 'egg', 'spam', 'ham'

# This function will run when pressing the space bar
def on_spacebar_press( event ):
    print( 'Clicked words:', *clicked )

# Button creator function
def create_buttons( words ):
    # Create a button for each word
    for word in words:
        # This function will run when a button is clicked
        def on_button_click(word=word):
            clicked.add( word )
        # Add button
        button = tkinter.Button( app,
                                 text=word,
                                 command=on_button_click )
        # If you have 180 buttons, you should consider using the grid()
        # layout instead of pack() but for simplicity I used this one for demo
        button.pack()

# Binding function tp space bar event
app.bind('<space>', on_spacebar_press)

# This call creates the buttons
create_buttons( words )

# Now we enter to event loop -> the program is running
app.mainloop()