简体   繁体   English

如何检测鼠标单击是否选择了下面显示的颜色

[英]How to detect if the mouse click choosed an color displayed bellow

I would like to know the command to detect if the user has clicked on an asked color in tkinter, here's my program:我想知道检测用户是否在 tkinter 中点击了询问的颜色的命令,这是我的程序:

from tkinter import*
from random import*

fenetre=Tk()
fenetre.title('Jeu Chlepko Peter')
fenetre.geometry('500x280+500+280')

zone=Canvas(fenetre,width=500,height=100)
zone.grid(row=3, rowspan=3, column=0, columnspan=3)
zone.create_rectangle(0,0,500,200,fill='Grey')
couleurs=['Red','Green','Blue','Yellow','White','Purple','Pink','Black','Cyan','Orange']
shuffle(couleurs)



def jouer():
    zone.total=0
    zone.create_rectangle(0,0,500,200,fill='Grey')
    zone.alea=randint(0,4)
    zone.create_oval(0,100,100,0,fill=couleurs[zone.alea])
    zone.create_oval(200,100,100,0,fill='Blue')
    zone.create_oval(300,100,200,0,fill='White')
    zone.create_oval(400,100,300,0,fill='Black')
    zone.create_oval(500,100,400,0,fill='Orange')



bouton_jouer=Button(fenetre,text="Lancer le jeu",command=jouer)
bouton_jouer.grid(row=1,column=1)

description=Label(fenetre,text='')
description.grid(row=2,column=1)



bouton_quitter=Button(fenetre,text='Quitter', command=fenetre.destroy)
bouton_quitter.grid(row=7,column=1)

description=Label(fenetre,text='Cliquez sur la couleur demandée')
description.grid(row=0, column=1)

enter code here

fenetre.mainloop() 

I will be very pleased if you could respond me如果你能回复我,我会很高兴

I've added a onClick function to your code.我在您的代码中添加了一个 onClick 函数。 This function receives from tkinter which widget called it along with some information like the x,y coordinates.这个函数从 tkinter 接收哪个小部件调用它以及一些信息,比如 x,y 坐标。 I've used this to find the nearest object in the Canvas to where the user clicked.我已经使用它来查找 Canvas 中距离用户单击位置最近的对象。 Then I use itemcget to tell what color that object is and print the color to the terminal output.然后我使用 itemcget 告诉该对象是什么颜色并将颜色打印到终端输出。 You can then use this for whatever you need然后您可以将其用于任何您需要的

from tkinter import *
from random import *

fenetre=Tk()
fenetre.title('Jeu Chlepko Peter')
fenetre.geometry('500x280+500+280')

zone=Canvas(fenetre,width=500,height=100)
zone.grid(row=3, rowspan=3, column=0, columnspan=3)
zone.create_rectangle(0,0,500,200,fill='Grey')
couleurs=['Red','Green','Blue','Yellow','White','Purple','Pink','Black','Cyan','Orange']
shuffle(couleurs)


def onClick(event):
    item = event.widget.find_closest(event.x,event.y)
    color = event.widget.itemcget(item,'fill')
    print(color)

def jouer():
    zone.total=0
    zone.create_rectangle(0,0,500,200,fill='Grey')
    zone.alea=randint(0,4)
    zone.create_oval(0,100,100,0,fill=couleurs[zone.alea])
    zone.create_oval(200,100,100,0,fill='Blue')
    zone.create_oval(300,100,200,0,fill='White')
    zone.create_oval(400,100,300,0,fill='Black')
    zone.create_oval(500,100,400,0,fill='Orange')



bouton_jouer=Button(fenetre,text="Lancer le jeu",command=jouer)
bouton_jouer.grid(row=1,column=1)

description=Label(fenetre,text='')
description.grid(row=2,column=1)

zone.bind('<Button-1>',onClick)

bouton_quitter=Button(fenetre,text='Quitter', command=fenetre.destroy)
bouton_quitter.grid(row=7,column=1)

description=Label(fenetre,text='Cliquez sur la couleur demandée')
description.grid(row=0, column=1)

fenetre.mainloop() 

What I understand that you want to choose 4 colors from the couleurs list in each game when the user click the Lancer le jeu button.据我了解,当用户单击Lancer le jeu按钮时,您希望从每个游戏的couleurs列表中选择 4 种颜色。 Then you want to randomly choose one from the 4 chosen colors and ask the user to click on the correct color.然后你想从 4 种选择的颜色中随机选择一种,并要求用户点击正确的颜色。

Below is a modified version of your posted code to achieve the goal:以下是您发布的代码的修改版本,以实现目标:

from tkinter import *
from random import randint, shuffle

fenetre=Tk()
fenetre.title('Jeu Chlepko Peter')
fenetre.geometry('550x280+500+280')

zone=Canvas(fenetre,width=550,height=100)
zone.grid(row=3, rowspan=3, column=0, columnspan=3)
zone.create_rectangle(0,0,550,200,fill='Grey')

couleurs=['Red','Green','Blue','Yellow','White','Purple','Pink','Black','Cyan','Orange']

def on_circle_click(event):
  # get the item being clicked
  item = event.widget.find_withtag(CURRENT)[0]
  # get its color
  color = event.widget.itemcget(item, 'fill')
  print(color)
  if color == couleurs[zone.alea]:
    # do whatever you want
    print('correct color selected')
  else:
    print('oops! try again!')

# bind mouse click event to items with tag 'circle' only
zone.tag_bind('circle', '<Button-1>', on_circle_click)

def jouer():
  shuffle(couleurs) # rearrange the colors
  zone.alea = randint(0, 3) # get a random number between 0 and 3
  # remove existing circles
  zone.delete('random', 'circle')
  # create the circle with target color
  zone.create_oval(0,100,100,0,fill=couleurs[zone.alea], tags='random')
  # create circles with the first four colors in the color list with tag 'circle'
  zone.create_oval(250,100,150,0,fill=couleurs[0], tag='circle')
  zone.create_oval(350,100,250,0,fill=couleurs[1], tag='circle')
  zone.create_oval(450,100,350,0,fill=couleurs[2], tag='circle')
  zone.create_oval(550,100,450,0,fill=couleurs[3], tag='circle')

bouton_jouer=Button(fenetre,text="Lancer le jeu",command=jouer)
bouton_jouer.grid(row=1,column=1)

description=Label(fenetre,text='')
description.grid(row=2,column=1)

bouton_quitter=Button(fenetre,text='Quitter', command=fenetre.destroy)
bouton_quitter.grid(row=7,column=1)

description=Label(fenetre,text='Cliquez sur la couleur demandée')
description.grid(row=0, column=1)

fenetre.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM