我怎样才能设置一个按钮给我 tkinter 中条目的答案?

发布于 2025-01-11 00:17:34 字数 1790 浏览 2 评论 0原文

我尝试编写一个简单的代码来和我的朋友们开个玩笑。它的基础是:他们输入她的名字,当他们单击按钮时,就会出现答案。在此之前,我根据以下内容编写了一个简单的代码:if(xxxx), so print(xxxxx)。现在,我使用 tkinter 为我的代码提供 GUI。但是,我不知道当我按下按钮时如何让按钮执行此操作。


from tkinter import *

def piadinhas():
    nome = input('Say your name! ')
    if nome == 'Fabricia' or nome == 'fabricia':
        print('*****************')
    elif nome == 'Roberto' or nome == 'roberto':
        print('Para de camperar na caixa da B')
    elif nome == 'Lucas' or nome == 'lucas':
        print('Oi, mozão!')
    elif nome == 'Leonardo' or nome == 'leonardo':
        print('Porque você não planta *********************?')
    elif nome == 'Montanha' or nome == 'montanha':
        print('Seu nome é montanha, ******?')
    elif nome == 'Marcos' or nome == 'marcos' or nome == 'Marcos Cesar':
        print('SOCRAAAAAAAAAAAAAAAAAAAAAAAAM, e aquela festinha lá, hein!?')
    elif nome == 'Lorenzo' or nome == 'lorenzo' or nome == 'oznerol':
        print('Qual o seu dom?')
    elif nome == 'Camiran':
        print('Quando será o próximo meme de marea pegando fogo?')
    else:
        print('Você não é um dos ****!')


janela = Tk()
janela.title('Aplicativo de Adjetivos Carinhosos')
janela.geometry('300x300')
janela.config(background='#dde')

texto_apresentacao = Label(janela, text='Eu sei quem você é!', background='#dde')
texto_apresentacao.grid(column=0, row=0, padx=85, pady=15)
texto_orientacao = Label(janela, text='Say Your Name, Dont Lie', background='#dde')
texto_orientacao.grid(column=0, row=1, padx=85, pady=15)

entrada_nome = Entry(janela)
entrada_nome.grid(column=0, row=2, padx=85, pady=15)

botao_resultado = Button(janela, width=20, height=2)

botao_resultado.grid(column=0, row=3, padx=85, pady=15)


janela.mainloop()

I tried to make a simple code to do a joke with my friends. It's based in: they entry with her names, and when they clicked in button, a answer appears. Before this, I make a simple code based in: if(xxxx), so print(xxxxx). Now, I using tkinter to give a GUI to my code. But, I don't know how make the button do this when I push him.


from tkinter import *

def piadinhas():
    nome = input('Say your name! ')
    if nome == 'Fabricia' or nome == 'fabricia':
        print('*****************')
    elif nome == 'Roberto' or nome == 'roberto':
        print('Para de camperar na caixa da B')
    elif nome == 'Lucas' or nome == 'lucas':
        print('Oi, mozão!')
    elif nome == 'Leonardo' or nome == 'leonardo':
        print('Porque você não planta *********************?')
    elif nome == 'Montanha' or nome == 'montanha':
        print('Seu nome é montanha, ******?')
    elif nome == 'Marcos' or nome == 'marcos' or nome == 'Marcos Cesar':
        print('SOCRAAAAAAAAAAAAAAAAAAAAAAAAM, e aquela festinha lá, hein!?')
    elif nome == 'Lorenzo' or nome == 'lorenzo' or nome == 'oznerol':
        print('Qual o seu dom?')
    elif nome == 'Camiran':
        print('Quando será o próximo meme de marea pegando fogo?')
    else:
        print('Você não é um dos ****!')


janela = Tk()
janela.title('Aplicativo de Adjetivos Carinhosos')
janela.geometry('300x300')
janela.config(background='#dde')

texto_apresentacao = Label(janela, text='Eu sei quem você é!', background='#dde')
texto_apresentacao.grid(column=0, row=0, padx=85, pady=15)
texto_orientacao = Label(janela, text='Say Your Name, Dont Lie', background='#dde')
texto_orientacao.grid(column=0, row=1, padx=85, pady=15)

entrada_nome = Entry(janela)
entrada_nome.grid(column=0, row=2, padx=85, pady=15)

botao_resultado = Button(janela, width=20, height=2)

botao_resultado.grid(column=0, row=3, padx=85, pady=15)


janela.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2025-01-18 00:17:34

请参阅此处了解 Tkinter 中的 Entry 小部件。

声明一个字符串变量(需要存储用户的输入)。然后,将该字符串变量设置为输入框的文本变量。并且,设置“command=piadinhas”,以便在单击该按钮时调用该函数。

inp = StringVar()
entrada_nome = Entry(janela,textvariable = inp)
...

botao_resultado = Button(janela, width=20, height=2, command=piadinhas)

另外,请在您的函数中进行以下更改:

def piadinhas():
    nome = inp.get()

阅读文档了解更多信息。

Refer here to know about the Entry widget in Tkinter.

Declare a string variable (needed to store the user's input.) Then, make that string variable as the Entry box's textvariable. And, set "command=piadinhas" so that the function is called on clicking that button.

inp = StringVar()
entrada_nome = Entry(janela,textvariable = inp)
...

botao_resultado = Button(janela, width=20, height=2, command=piadinhas)

Also, make these changes in your function:

def piadinhas():
    nome = inp.get()

Read the documentation to know more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文