Python 带有if或else命令的Tkinter按钮

Python 带有if或else命令的Tkinter按钮,python,button,tkinter,Python,Button,Tkinter,我相信这对你来说是一个非常容易的问题,但对我来说却是一个巨大的问题 我不知道如何使用if和else为tkinter按钮创建命令。 事实上,我的想法是,当我按下按钮后在条目上输入“白色”一词时,它必须向我显示带有“右”的标签,但使用此代码时,按钮总是向我显示错误……为什么 代码如下: # coding=utf-8 from tkinter import * from tkinter import font from PIL import ImageTk,Image import time from

我相信这对你来说是一个非常容易的问题,但对我来说却是一个巨大的问题

我不知道如何使用if和else为tkinter按钮创建命令。 事实上,我的想法是,当我按下按钮后在条目上输入“白色”一词时,它必须向我显示带有“右”的标签,但使用此代码时,按钮总是向我显示错误……为什么

代码如下:

# coding=utf-8
from tkinter import *
from tkinter import font
from PIL import ImageTk,Image
import time
from PIL import ImageTk,Image

schermata = Tk()
ment = StringVar()

schermata.geometry('750x500')
schermata.title('DANTE')

def comando():
    global input
    if ment=='white':
        time.sleep(1)
        risposta_giusta.pack()
        risposta_giusta.place(x=20, y=290)
    else:
        time.sleep(1)
        risposta_sbagliata.pack()
        risposta_sbagliata.place(x=20,y=290)

def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width, new_height))
    photo = ImageTk.PhotoImage(image)
    label.config(image = photo)
    label.image = photo #avoid garbage collection

image = Image.open('dante.gif')
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = Label(schermata, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)

font = ('Ubuntu',22)
font_2 = ('Ubuntu',12)
labelfont = ('Noto Serif CJK SC', 35, 'bold')
titolo = Label(schermata,text='DANTE',bg='#E5E3C6',fg='#BF000D')
titolo.config(font=labelfont)
titolo.pack(fill=BOTH, expand = YES)
titolo.place(x=20, y=20)

risposta_giusta = Label(schermata, text='RIGHT!!', font=font, bg='#E5E3C6',fg='#0F0000')
risposta_sbagliata = Label(schermata, text='WRONG', font=font, bg='#E5E3C6',fg='#0F0000')

domanda = Label(schermata, text="what is the color of Napoleone's horse?", font=font_2, bg='#E5E3C6',fg='red')
domanda.pack()
domanda.place(x=20, y=120)

input = Entry(schermata,textvariable=ment,bg='white',width=23,disabledbackground='black')
input.pack(ipady=3)
input.place(x=20, y=190)

bottone = Button(schermata,text='PARLA CON DANTE',command=comando,bg='#BF000D',fg='white')
bottone.pack()
bottone.place(x=20, y=245)

schermata.mainloop()
#编码=utf-8
从tkinter进口*
从tkinter导入字体
从PIL导入ImageTk,图像
导入时间
从PIL导入ImageTk,图像
schermata=Tk()
ment=StringVar()
schermata.几何体('750x500')
schermata.标题(“但丁”)
def comando():
全局输入
如果颜色==‘白色’:
时间。睡眠(1)
Rispesta_giusta.pack()
Rispesta_giusta.地点(x=20,y=290)
其他:
时间。睡眠(1)
Rispesta_sbagliata.pack()
Rispesta_sbagliata.地点(x=20,y=290)
def resize_图像(事件):
新建宽度=event.width
新高度=事件高度
图像=复制图像。调整大小((新宽度,新高度))
photo=ImageTk.PhotoImage(图像)
label.config(image=photo)
label.image=photo#避免垃圾收集
image=image.open('dante.gif')
copy\u of_image=image.copy()
photo=ImageTk.PhotoImage(图像)
标签=标签(schermata,图像=照片)
label.bind(“”,调整图像大小)
label.pack(填充=两者,展开=是)
字体=('Ubuntu',22)
字体2=('Ubuntu',12)
标签字体=('Noto Serif CJK SC',35',粗体')
titolo=标签(schermata,text='DANTE',bg='#E5E3C6',fg='#BF000D')
titolo.config(font=labelfont)
titolo.pack(填充=两者,扩展=是)
标题位置(x=20,y=20)
risposta_giusta=Label(schermata,text='RIGHT!!',font=font,bg='#E5E3C6',fg='#0F0000')
risposta_sbagliata=标签(schermata,text='error',font=font,bg='#E5E3C6',fg='#0F0000')
domanda=Label(schermata,text=“拿破仑的马是什么颜色的?”,font=font\u2,bg=''E5E3C6',fg='red')
domanda.pack()
多曼达广场(x=20,y=120)
输入=输入(schermata,textvariable=ment,bg='white',width=23,disabledbackground='black')
input.pack(ipady=3)
输入位置(x=20,y=190)
按钮(schermata,text='PARLA condante',command=comando,bg='#BF000D',fg='white')
bottone.pack()
位置(x=20,y=245)
schermata.mainloop()

在您的代码中,
ment
是一个tk
StringVar()
对象。要读取变量的值,需要使用
.get()
方法:

def comando():
    global input
    if ment.get()=='white':
        time.sleep(1)
        risposta_giusta.pack()
        risposta_giusta.place(x=20, y=290)
如果您打印出
ment
,您会注意到它会显示
PY_VAR0
或类似内容(这是字符串变量对象的Tcl名称),因此它永远不会等于
'white'

尝试
If-ment.get()='white':
下的
comando()