Python Forum
tkinter button not accessing the command when clicked
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter button not accessing the command when clicked
#1
I am making a tkinter program where I want one button (button1) always on the screen. Every time that button is clicked I want a new button (button2) to form in a different area of the screen, each time the first button (button1) is clicked a new button (button2) should appear 30 pixels below the previous one. To do this I am using a counter. Where I am having an issue is when any one of the new buttons (button2) are clicked, I want the counter to reset. In other words, now when button1 is clicked, button2 should appear in the original spot again because the counter is 0 again. For some reason though my counter is never reseting and the new buttons (button2) just keep appearing lower than the last even after one of them is clicked. Please help thank you!

My code looks something like this:

from tkinter import *

window = Tk()

counter = 0
def resetCounter(counter):
   counter = 0

def button1Clicked():
   global counter
   button2 = Button(window, text="click me when ready", command(resetCounter(counter)))
   button2.place(x=50, y=(100 + (counter*30))
   counter += 1

button1 = Button(window, text="click me" command(button1Clicked))
button1.place(x=0, y=0)

window.mainloop()
Reply
#2
I have corrected your code so it will run but I am not sure what it is exactly that you are trying to do.

As I currently have it running the window opens with the "Click Me" button showing in the upper right corner "x=0,y=0", as you have it preset. When button1 is clicked the new button (button2), displays at "x=50, y=100" while button1 remains in the window.

When button2 is clicked the code then executes "resetCounter(counter)" which then assigns the value of "0" to the variable.

So as I see the code running it is performing what you are stating, or am I missing what you are trying to do here?

from tkinter import *

window = Tk()

counter = 0
def resetCounter(counter):
   counter = 0

def button1Clicked():
   global counter
   button2 = Button(window, text="click me when ready", command = resetCounter(counter))
   button2.place(x=50, y=100), + (counter*30)
   counter += 1

button1 = Button(window, text='Click Me', command = button1Clicked)
button1.place(x=0,y=0)

window.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Continue command in python tkinter? MLGpotato 7 354 Mar-27-2021, 04:59 AM
Last Post: deanhystad
  tkinter button image Nick_tkinter 4 290 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 510 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 651 Jan-26-2021, 06:38 AM
Last Post: zazas321
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 330 Dec-28-2020, 10:00 PM
Last Post: nanok66
  Get name of command button Heyjoe 3 365 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 2,419 Oct-18-2020, 10:58 AM
Last Post: TurboC
  tkinter | Button color text on Click Maryan 2 534 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [tkinter] not getting checkbutton value when clicked OogieM 5 609 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  Class function does not create command button Heyjoe 2 479 Aug-22-2020, 08:06 PM
Last Post: Heyjoe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020