How to hide a button after clicked in Python

2024/7/8 8:44:24

I was wondering how to hide my start button after being clicked so that If the user accidentally was clicker happy they wouldn't hit the button causing more bubbles to appear on screen. Below is a snippet of the coding using Python 3.3:

    from tkinter import *import randomfrom tkinter.messagebox import showinfoclass BFrame:def __init__(self, root, name):self.name = nameroot.title("Math Bubbles")self.bubbles = {} # this will hold bubbles ids, positions and velocitiesself.score = 0Button(root, text="Start", width=8, bg="Pink", command=self.make_bubbles).pack() # This button starts the game, making the bubbles move across the screenButton(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 gameself.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 variablecolour = random.choice(COLOURS) # This picks a colour randomlyoval_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 dictionarydef click(self, event):if self.canvas.find_withtag(CURRENT):item_uid = event.widget.find_closest(event.x, event.y)[0]is_even = Falsetry: # clicked ovalself.bubbles[item_uid]except KeyError: # clicked ovalfor key, value in self.bubbles.iteritems():if item_uid == value[5]: # comparing to text_idif value[4] % 2 == 0:is_even = Trueself.canvas.delete(key) # deleting ovalself.canvas.delete(item_uid) # deleting textelse:if self.bubbles[item_uid][4] % 2 == 0:is_even = Trueself.canvas.delete(item_uid) # deleting ovalself.canvas.delete(self.bubbles[item_uid][5]) # deleting textif is_even:self.score += 1else:self.score -= 1showinfo("Oh no!", "%s! You clicked the wrong bubble, please start again." % self.name)if self.score == 10:#Tells user You won! if score is 10showinfo("Winner", "You won %s!" % self.name)self.canvas.delete(self.current_score)#Shows updated score on canvasself.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: %s"%self.score)
Answer

You can switch the state of the button in the handler:

import Tkinter as tkclass 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.DISABLEDmaster = tk.Tk()
a = App()
master.mainloop()

(python2.7 code, but it should translate to py3k pretty easily).

https://en.xdnf.cn/q/119448.html

Related Q&A

Unable to click on QRadioButton after linking it with QtCore.QEventLoop()

Few days back i had situation where i had to check/uncheck QRadioButton in for loop. Here is the link Waiting in for loop until QRadioButton get checked everytime? After implementing QEventLoop on thi…

Distance Matrix Haversine

I am working on a data frame that looks like this :lat lon id_zone 0 40.0795 4.338600 1 45.9990 4.829600 2 45.2729 2.882000 3 45.7336 4.850478 4 45.6981 5.…

python google geolocation api using wifi mac

Im trying to use Googles API for geolocation giving wifi data to determine location. This is their intro. And this is my code@author: Keith """import requestspayload = {"c…

Python requests and variable payload

Reticulated members,I am attempting to use a GET method that is supported against the endpoint. However, I am using python and wanting to pass the user raw_input that is assigned to a variable:uid = ra…

How should I read and write a configuration file for TkInter?

Ive gathered numbers in a configuration file, and I would like to apply them to buttons. Clicking the button should allow the number to be changed and then re-written to the config file. My current cod…

How to convert this nested dictionary into one single dictionary in Python 3? [duplicate]

This question already has answers here:Convert nested dictionary into a dictionary(2 answers)Flatten nested dictionaries, compressing keys(32 answers)Closed 4 years ago.I have a dictionary like this:a …

How to click unopened tabs where the numbers change

How do I click all the unopened tabs pages where the value changes when you click tabs? (see image below)Take the following script, based off of this question with the following approach:clickMe = wai…

Xlwings / open password protected worksheet in xlsx?

I get an answer how to open a password protected xlsx with xlwings: Xlwings / open password protected xlsx? wb = xlwings.Book("file.xlsx", password="Passw0rd!")But can i also open …

Wrapping an opencv implementaion of an error level analysis algorithm using cython

i have implemented an error level analysis algorithm using c++(opencv version 2.4) and i want to build a python wrapper for it using cython. I have read some part of the documentation of cython for c++…

Setting yticks Location Matplotlib

Im trying to create a plot which has y axis exactly same with this : And Im in this situation and everything is ok until this point:When i try this lines:ax.set_yticks(range(20,67,10)) ax.set_yticklabe…