1

I am trying to create a car configurator using tkinter as a gui in my free time.

I have managed to open a tkinter box with images that act as buttons.

What I want to do is for the user to click on a button. I want to check which button has been clicked (i.e if the family car button is clicked, how can I check that it has been clicked).

I have done my research on this website, and all of the solutions I have found have been in javascript or other languages.

Once the button has been clicked, I want a new window to be opened ONLY containing attributes for a family car i.e a family car can have a red exterior colour, but a sports car cannot have a red exterior colour at all.

Here is my code below:

from tkinter import * import tkinter as tk def create_window(): window = tk.Toplevel(root) root = tk.Tk() familycar = PhotoImage(file = "VW family car.png") familylabel = Button(root, image=familycar) familybutton = Button(root, image=familycar, command=create_window) familybutton.pack() 

So how can I check that the family car button has been clicked?

Thanks

3
  • Your create_window() function is called whenever the familybutton has been clicked. In that function you can do whatever you want to do when the familybutton is clicked, create the window you want. Why would you need to check if it has been clicked at any other time? Commented Aug 9, 2018 at 12:01
  • You have a very solid point. I have just tried the code 'window.title("Exterior colour"), and when I clicked on 'familybutton', the new window opened with the title 'exterior colour'. I think I now know what I need to do. Thanks Commented Aug 9, 2018 at 12:08
  • You're welcome. I can see where the feeling of needing to check comes from, but basically Tkinter does this for you in its mainloop. This mainloop basically checks if there is an event, does what it is told to do when this event happens, updates the window, repeat. So all you have to do is tie your function to the button press event, which you do with the command=. Do keep in mind that returning values from a button press function isn't possible. Therefore, people tend to put Tkinter applications in classes, as explained in this answer. Commented Aug 9, 2018 at 13:06

3 Answers 3

1

Use a Boolean flag.

Define isClicked as False near the beginning of your code, and then set isClicked as True in your create_window() function.

This way, other functions and variables in your code can see whether the button's been clicked (if isClicked).

Sign up to request clarification or add additional context in comments.

5 Comments

Is isClicked a variable I have to define?
@theoneandonylndy Yep :)
Ok I shall try that then. Thank you :)
Can i then use if statements to check if it has been clicked?
@theoneandonylndy Yep
0

Not sure what you asked, do you want to disable it or check its status in another routine ? Or just to count the times it has been clicked,

In order to do that Simple solution would be to add a general variable that will be updated inside the create_window method (general because you want to allow access from other places).

1 Comment

In general terms, if the user has clicked on the family car button, I want to check if it has been clicked on, so I can then add the other attributes that make a family car i.e alloy colour, interior colour.
0

First, you would need to initialize a function in which you want to execute on the button click.

example:

def button_clicked(): print('I got clicked') 

Then, when you define the target button, you'll have to set the 'command' argument to the required function(the button_clicked function in this context).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.