我怎样才能设置一个按钮给我 tkinter 中条目的答案?
我尝试编写一个简单的代码来和我的朋友们开个玩笑。它的基础是:他们输入她的名字,当他们单击按钮时,就会出现答案。在此之前,我根据以下内容编写了一个简单的代码: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅此处了解 Tkinter 中的 Entry 小部件。
声明一个字符串变量(需要存储用户的输入)。然后,将该字符串变量设置为输入框的文本变量。并且,设置“command=piadinhas”,以便在单击该按钮时调用该函数。
另外,请在您的函数中进行以下更改:
阅读文档了解更多信息。
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.
Also, make these changes in your function:
Read the documentation to know more.