2013-07-19 31 views
1

我不能讓鼠標在我的程序中工作。我希望能夠點擊氣泡,我做了一個數字列表,是「2的倍數」這個問題的答案(不知道這是否有效)。如果用戶點擊包含2的倍數的泡泡,我希望泡泡消失,並且得分增加1(因挫折而未得分)。反之亦然(如果用戶點擊錯誤的泡泡號碼,分數將下降)。Python:如何獲得使用鼠標點擊工作?

使用Python 3.3的代碼 -

from tkinter import * 
import random 

class MouseClick: 

    def MakeChoice (event): 
     if multiples2 pressed = score + 1 
     else score -1 

回答

2

我不是一個Tkinter的專家,只是試圖解決。我測試了Python 2.7,但我認爲它會在Python3上運行。

from Tkinter import * 
import random 


class BubbleFrame: 

    def __init__(self, root): 
     root.title("Math Bubbles") 
     self.bubbles = {} # this will hold bubbles ids, positions and velocities 
     self.score = 0 
     Button(root, text="Start", width=8, command=self.initialize_bubbles).pack() # This button starts the game, making the bubbles move across the screen 
     Button(root, text="Quit", width=8, command=quit).pack() 
     self.canvas = Canvas(root, width=800, height=650, bg='#afeeee') 
     self.canvas.create_text(400, 30, fill="darkblue", font="Times 20 italic bold", text="Click the bubbles that are multiples of two.") 
     self.canvas.pack() 

    def initialize_bubbles(self): 
     for each_no in xrange(1, 21): 
      xval = random.randint(5, 765) 
      yval = random.randint(5, 615) 
      oval_id = self.canvas.create_oval(xval, yval, xval + 30, yval + 30, 
               fill="#00ffff", outline="#00bfff", 
               width=5, tags="bubble") 
      text_id = self.canvas.create_text(xval + 15, yval + 15, text=each_no, tags="bubble") 
      self.canvas.tag_bind("bubble", "<Button-1>", lambda x: self.click(x)) 
      self.bubbles[oval_id] = (xval, yval, 0, 0, each_no, text_id) # add bubbles to dictionary 

    def click(self, event): 
     if self.canvas.find_withtag(CURRENT): 
      item_uid = event.widget.find_closest(event.x, event.y)[0] 
      is_even = False 
      try: # clicked oval 
       self.bubbles[item_uid] 
      except KeyError: # clicked the text 
       for key, value in self.bubbles.iteritems(): 
        if item_uid == value[5]: # comparing to text_id 
         if value[4] % 2 == 0: 
          is_even = True 
         self.canvas.delete(key) # deleting oval 
         self.canvas.delete(item_uid) # deleting text 
      else: 
       if self.bubbles[item_uid][4] % 2 == 0: 
        is_even = True 
       self.canvas.delete(item_uid) # deleting oval 
       self.canvas.delete(self.bubbles[item_uid][5]) # deleting text 
      if is_even: 
       self.score += 1 
      else: 
       self.score -= 1 
      print self.score 

    def loop(self, root): 
     for oval_id, (x, y, dx, dy, each_no, text_id) in self.bubbles.items(): 
      # update velocities and positions 
      dx += random.randint(-1, 1) 
      dy += random.randint(-1, 1) 
      # dx and dy should not be too large 
      dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5)) 
      # bounce off walls 
      if not 0 < x < 770: 
       dx = -dx 
      if not 0 < y < 620: 
       dy = -dy 
      # apply new velocities 
      self.canvas.move(oval_id, dx, dy) 
      self.canvas.move(text_id, dx, dy) 
      self.bubbles[oval_id] = (x + dx, y + dy, dx, dy, each_no, text_id) 
     # have mainloop repeat this after 100 ms 
     root.after(100, self.loop, root) 

if __name__ == "__main__": 
    root = Tk() 
    frame = BubbleFrame(root) 
    frame.loop(root) 
    root.mainloop() 
+0

感謝您的幫助!我只需稍作調整即可使其在Python 3.3上運行。無論如何,我可以在畫布上做記分板表演嗎? – LilKasian

+0

您可以使用具有隻讀配置的「文本」來更新分數。 – Babu