0

I have been trying to execute 2 commands in 1 button. I have read that using lambda can solve the problem. But my situation is a little different. On button press, one command is to destroy the existing GUI, and the second command, I want is to open another SERVER GUI. Below is my existing button functionality.

exit_button = Button(topFrame, text='Quit', command=window.destroy)

How can I open another GUI using same button?

Thank you for any help.

Edit:-

I have now created the below function:

def close_func():
os.kill(os.getpid(), signal.SIGINT)
GUI_Interface()
window.destroy()
server_socket.close()

GUI_Interface is a function that I need to call after closing the existing .py file. If I put GUI_Interface as the first command for close_func(), then it really does not go back to the 2nd step and never closes the existing.py file.

And if I place GUI_Interface in the end, it just closes the existing one and nevr opens the function of the new .py file

Madhav
  • 35
  • 11

1 Answers1

0

Three options at least:

using or (with lambda if arguments):

from tkinter import Tk, Button


root = Tk()

Button(root, text='Press', command=lambda: print('hello') or root.destroy() or print('hi')).pack()

root.mainloop()

important to use or because and didn't work (don't know why or how this works at all)

or use function definitions:

from tkinter import Tk, Button


def func():
    print('hello')
    root.destroy()
    print('hi')


root = Tk()

Button(root, text='Press', command=func).pack()

root.mainloop()

or lists with lambda:

from tkinter import Tk, Button


def func():
    print('hello')
    root.destroy()
    print('hi')


root = Tk()

Button(root, text='Press', command=lambda: [print('hello'), root.destroy()]).pack()

root.mainloop()
Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • lambda can contain a list of commands. like: `lambda:[cmd1(),cmd2()..]` – Thingamabobs Apr 13 '21 at 19:10
  • @Atlas435 You learn something new every day – Matiiss Apr 13 '21 at 19:11
  • 2
    @Atlas435: Putting multiple function calls in a list then in lambda is a very bad practice. 2-3 extra lines of code won't harm anything instead it improves the quality of programming. – Saad Apr 13 '21 at 19:17
  • umm... i am actually trying to open another .py file using the same button. it will destroy the existing .py file GUI and then open the new .py file GUI. – Madhav Apr 13 '21 at 19:17
  • lambda helps with function combination but what I am trying to combine is:- another .py file after closing the existing .py file – Madhav Apr 13 '21 at 19:18
  • @Madhav because it contains `root.destroy()` which destroy the Tk() – Matiiss Apr 13 '21 at 19:18
  • @Madhav You can simply import stuff from the other `.py` file e.g. a function and call it when needed. – Matiiss Apr 13 '21 at 19:19
  • @Matiiss so I have a function named GUI_Interface() in another .py file. I can simply do:- import GUI_Interface and then use that function in button functionality? – Madhav Apr 13 '21 at 19:21
  • @Saad Thanks for letting me know. – Thingamabobs Apr 13 '21 at 19:24
  • @Matiiss thank you for your answer. it has actually solved my problem by importing the required function and then using a new function definition. – Madhav Apr 13 '21 at 19:32
  • @Madhav You have to do either `from file_name import function, class, etc` or `import file_name` tho the file has to be in the same directory else do `from path.to.file_name import class, func, variable, *` or `import path.to.file_name` – Matiiss Apr 13 '21 at 19:32
  • @Matiiss yup the import helped. Thank you – Madhav Apr 13 '21 at 19:34
  • def close_func(): window.destroy() os.kill(os.getpid(), signal.SIGINT) server_socket.close() GUI_Interface() @Matiiss This is what i am doing right now... It is killing the existing .py file but not opening the new .py file... Any ide what I am doing wrong here – Madhav Apr 16 '21 at 00:17
  • how do You expect it to open a new .py file? also try first opening the GUI then destroy it, also also threads will be closed if not used or something like that don't remember the specifics but at some point they may get garbage collected., would be nice since it is a separate problem (it is right? it is not related to calling two functions by a button?) ask another qeustion and show the code properly – Matiiss Apr 16 '21 at 06:51
  • when You ask, You might as well post the link here too so that I can take a look – Matiiss Apr 16 '21 at 06:52
  • @Matiiss it is related to the same problem... i am still not able to open new function. If i open the function from another .py file, then it starts the functionality of .py file but does not really terminate the main .py file. – Madhav Apr 16 '21 at 15:36
  • first of it probably won't terminate the main file since then it will terminate the whole process, what You can do tho is add `if __name__ == '__main__':` to the other .py file and put all the callables under there so that it doesn't execute on import, also could You provide how You import and try terminating the first .py file (it would be great if You just asked another question and linked here so that this quesiton is reserved to the specific problem of executing multiple functions with a button) – Matiiss Apr 16 '21 at 17:31
  • @Matiiss I have created another question with my existing functionality. https://stackoverflow.com/questions/67011984/unable-to-open-another-py-file-on-exiting-the-primary-py-file – Madhav Apr 16 '21 at 20:26