-1

我想知道如何在点击后隐藏我的开始按钮,这样如果用户不小心点击者高兴,他们就不会点击按钮,导致屏幕上出现更多气泡。以下是使用 Python 3.3 的代码片段:

    from tkinter import *
    import random

    from tkinter.messagebox import showinfo

    class BFrame:

        def __init__(self, root, name):
            self.name = name
            root.title("Math Bubbles")
            self.bubbles = {} # this will hold bubbles ids, positions and velocities
            self.score = 0
            Button(root, text="Start", width=8, bg="Pink", command=self.make_bubbles).pack() # This button starts the game, making the bubbles move across the screen
            Button(root, text="Quit", width=8, bg="Yellow",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 answers in the two times tables.")
            #Shows score at beginning of the game
            self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: 0")
            self.canvas.pack()



def make_bubbles(self):
    for each_no in range(1, 21):
        xval = random.randint(5, 765)
        yval = random.randint(5, 615)
        COLOURS = ('#00ff7f', '#ffff00', '#ee82ee', '#ff69b4', '#fff0f5') # CAPS represents a constant variable
        colour = random.choice(COLOURS) # This picks a colour randomly
        oval_id = self.canvas.create_oval(xval, yval, xval + 60, yval + 60,fill=colour, outline="#000000", width=5, tags="bubble")
        text_id = self.canvas.create_text(xval + 30, yval + 30, 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 oval
                    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
                    showinfo("Oh no!", "%s! You clicked the wrong bubble, please start again." % self.name)

                if self.score == 10:
                    #Tells user You won! if score is 10
                    showinfo("Winner", "You won %s!" % self.name)


            self.canvas.delete(self.current_score)
            #Shows updated score on canvas
            self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: %s"%self.score)
4

2 回答 2

1

您可以在处理程序中切换按钮的状态:

import Tkinter as tk

class App(object):
    def __init__(self):
        self.b = tk.Button(master, text='foo', command=self.switch_state)
        self.b.pack()
    def switch_state(self):
        print("Called")
        self.b['state'] = tk.DISABLED

master = tk.Tk()
a = App()
master.mainloop()

(python2.7 代码,但它应该很容易转换为 py3k)。

于 2013-10-20T04:19:22.853 回答
-1

蟒蛇3.7

from tkinter import *

def toggle():
   locx, locy = label.winfo_x(), label.winfo_y()
   label.place(x=-locx,y=-locy)

master = Tk()
master.geometry("%dx%d+0+0" % (500,500))
label=Label(master,text="Lebel : Hide Me")
label.place(x=50,y=20)
btn=Button(text="Toggle",command=toggle)
btn.place(x=50,y=50)
mainloop()
于 2019-06-15T10:06:21.340 回答