Bottom Page

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()
Quote
#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()
"Often stumped... But never defeated."
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Problem with Submit button Tkinter Reldaing 2 130 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  Tkinter button in for Simpsunt 3 154 Nov-23-2019, 01:41 AM
Last Post: Larz60+
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 334 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [Tkinter] Tkinter button help and general commands Iceman 1 310 Sep-01-2019, 05:09 PM
Last Post: Yoriz
  [Tkinter] How to check after 30 minutes if Buttons have been clicked? bmghafoor 1 234 Aug-09-2019, 04:57 PM
Last Post: Yoriz
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 515 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] command button automaticaly fired kferhat 2 377 Jun-16-2019, 08:21 PM
Last Post: kferhat
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 755 May-12-2019, 08:17 PM
Last Post: joe_momma
  [Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments nonzzo 2 497 Apr-22-2019, 12:36 PM
Last Post: Larz60+
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 1,506 Apr-18-2019, 12:48 PM
Last Post: ShashankDS

Forum Jump:


Users browsing this thread: 1 Guest(s)