Python Forum
Continue command in python tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Continue command in python tkinter?
#1
Hello! Is there anyway to set a continue command to button without making another def?
enter = Button(root, text = "Name your self" command = #I need to continue the def I already have)
Reply
#2
Anywhere you use a function you can also use a lambda expression, since a lambda expression is just a different way of writing a function. But I don't think that is what you are asking. To be honest I have no idea what you are asking.

You use the words "continue" and "def". In Python, "continue" can be used to jump to the start of an enclosed loop. Is that what you mean by "continue"?

Another common usage of "continue" is to resume. Are you asking if you can use a button as a variable length wait? Something like this:
while not button_pressed:
    pass
# Do some things that were waiting for the button press
…
Button(root, 'Push me', command=button_pressed=True)
This is wrong in a couple of ways. The first error is that you cannot have "infinite" loops in a GUI program. The "while not button_pressed:" loop prevents mainloop() from running. If mainloop() does not run then the program will not recognize that you pushed the button. The entire GUI freezes.

Since waiting is mostly done by preventing anything else from happening, waiting is something to avoid in a GUI program. A GUI program is event driven. You do not wait for the user to do anything. The user performs actions which generate events (button presses, text entry etc...) and your program responds to the events to do what the user wants to do. So instead of:
do_A()
wait for button press
do_B()
You would have something like:
do_A()
Button(root, text="Push me", command=do_B())
Instead of a long stream of code that you expect to execute sequentially your program becomes a bunch of short functions that get bound to GUI controls. It is a completely different way of thinking about programming.
Reply
#3
So I dont want to make code like this:
def A():
root = Button(root, text = "BOOM", command = B())

def B():
  #stuff here
For me its better like this:
def A():
  root = Button(root, text = "BOOM", command = #Goes straight to hello world)
  print("Hello world!")
  #rest of the code
  #rest of the code
Reply
#4
You cannot do that.
Reply
#5
Hi MLG Potato!
I figured how to solve your problem, I hope you are fine with the code being like this:
def A():
def B():
print("Hello World")
button = Button(window, text="BOOM!", command=B)
button.pack()
Basically I'm putting the B function into the A function lol. I tried it and it works perfectly like you asked for! if you need more help on anything or have any questions on my code please contact me by commenting on this message! hope u have a marvelous day.
Reply
#6
That does not solve the problem. MLGpotato wants to use a button like a sleep command. He wants his A() function to draw a button, wait until the button is pressed, then return from function A. He does not want to have the button call a different function to perform some processing. What MLGpotato wants is not possible using tkinter. MLGpotato needs some education on how GUI programming works. That will point a bright spotlight on the fact that not only is his idea not possible, it is not desirable. Maybe his plans will change to use a dialog, or maybe the user interface will change to support a freer flow of control.
MLGpotato likes this post
Reply
#7
Nothing is impossible. And I don't know what you mean on what you're saying anyway.
Reply
#8
I guessed that since your answer doesn't work at all for the question in the original post. That's not surprising because what MLGpotato wants to do so is odd. He wants to write a function that creates a button, and then have code execution pause until the button is pressed.

It is like he wants to do this:
def A():
    print('Waiting for 10 seconds')
    time.sleep(10)
    print('Done sleeping')
But with a button.
def A():
    print('Push button after 10 seconds')
    tk.Button(root, text="Push Me", command=continue)
    print('Done sleeping')
MLGpotato would like the code to pause right after the button is created, and continue when the button is pressed. As far as I know, that is impossible in tkinter. I can make something that looks like that is what happened, but it uses a completely different mechanism.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,776 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  [Tkinter] button command tkinter Heyjoe 6 4,995 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  tkinter button not accessing the command when clicked jhf2 1 3,501 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [Tkinter] Tkinter newly created button does not execute command ChipsSlave 7 6,236 Jul-25-2018, 01:01 AM
Last Post: JUANCARLOS

Forum Jump:

User Panel Messages

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