有没有一种基于是否有人按下按钮(tkinter)的条件语句的方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有没有一种基于是否有人按下按钮(tkinter)的条件语句的方法相关的知识,希望对你有一定的参考价值。
这是我的代码:
root = tkinter.Tk()
canvas = tkinter.Canvas(root, width=960, height=720)
image = ImageTk.PhotoImage(Image.open('TitleScreen.png'))
canvas.create_image(0, 0, anchor=tkinter.NW, image=image)
canvas.create_text(485, 375, font=("Times", 25, 'bold'),
text='You live in Valencia, Venezuela. You have 2 kids, and a '
'\n'
'significant other. You barely have enough money to take care '
'\n'
'of your family, food is running out, and electricity outages '
'\n'
'are a daily occurrence. Do you want to leave in search '
'\n'
'of a better life? [y/n]', fill='white')
y = Button(root, text='Y', command=buttonFunctionY, bg='black', bd=5, font=('Times', 20), activebackground='blue',
activeforeground='black', fg='black')
y.place(x=435, y=465)
n = Button(root, text='N', command=buttonFunctionN, bg='black', bd=5, font=('Times', 20), activebackground='blue',
activeforeground='black', fg='black')
n.place(x=485, y=465)
if option == 'y':
canvas.create_image(0, 0, anchor=tkinter.NW, image=image)
canvas.create_text(485, 375, font=("Times", 25, 'bold'),
text='Great, good decision. First should you prepare for the trip by buying supplies? [y/n]',
fill='white')
canvas.pack()
root.mainloop()
我的主要问题临近尾声,我试图根据是否按下按钮来创建条件,但我不知道该怎么做。
答案
您必须在回调函数内而不是在按钮对象初始化下方定义单击时的反应。可能您已经定义了buttonFunctionY
,如果是,则添加行以在那里输出文本。否则,定义该功能(在按钮中使用该功能之前,但在导入之后)
def buttonFunctionY():
canvas.create_image(0, 0, anchor=tkinter.NW, image=image)
canvas.create_text(485, 375, font=("Times", 25, 'bold'),
text='Great, good decision. First should you prepare for the trip by buying supplies? [y/n]',
fill='white')
以上是关于有没有一种基于是否有人按下按钮(tkinter)的条件语句的方法的主要内容,如果未能解决你的问题,请参考以下文章