0

I am trying to create an application that can open another .py file(Tkinter application). I had tried the below code and it works for first time(it opens another application for the first time) but if i close that app and try to open it again by clicking on the open button then it doesn't open for second time.

Main Application:

from tkinter import *
root=Tk()
def ope():
  import SecondApp
Open_1=Button(root,text="Open",command=ope)
Open_1.pack()
root.mainloop()

SecondApp:

from tkinter import *
root1=Tk()
Label=Label(root1,text="Hey welcome in second app")
Label.pack()
root1.mainloop()

If i click the Open_1 button for first time from mainapp then it open second app in that time but if i try again then in second time it doesn't do anything. And in last I want to open the second window using import SecondApp Please Help me as soon as possible. Thanks for everyone who tried to solve my problem

imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • have u tried naming things not as same as class names in ur second app like `Label` variable to `label` because it can confuse tkinter and also the `root` in both modules is same right? mayb change one to `root1`? – Delrius Euphoria Jul 27 '20 at 12:32
  • Yeah that doesn't doesn't work and that's my question! thanks for try please read question again and answer – imxitiz Jul 27 '20 at 12:34
  • Put the main code of second file in a function, then import that function in first file and call that function instead. Note that module will be imported once, later import again will not import the module because the module has been imported already. – acw1668 Jul 27 '20 at 12:35
  • if uve tried it, rather than asking me to read the Q you should update it in d Q cuz no where is it mentioned that you have changed the variable names and gave it a try – Delrius Euphoria Jul 27 '20 at 12:36
  • @CoolCloud I had tried in my computer. And it doesn't work so i said it – imxitiz Jul 27 '20 at 12:38
  • puttin have u tried putting the second app inside a function and calling the function in the main app? – Delrius Euphoria Jul 27 '20 at 12:39
  • @acw1668 i can do that but i am trying to open more then 15 application using one app so. that wil not help me – imxitiz Jul 27 '20 at 12:39
  • No matter how many applications you want to import, your current design need to be changed, otherwise you never get what you want. – acw1668 Jul 27 '20 at 12:43
  • @acw1668 is that the last option? Isn't there any other way to do that? – imxitiz Jul 27 '20 at 12:45
  • @Kshitiz there is, but python and tkinter arent intended to work this way. Look at this [post][1] rather than this [post][2]. [1]: https://stackoverflow.com/questions/62755644/tkinter-cannot-open-main-window/62755770#62755770 [2]: https://stackoverflow.com/a/1254379/13629335 – Thingamabobs Jul 27 '20 at 14:02
  • @Atlas435 thankyou for helping me but i don't wanna do class and like that stuff. So, now i am doing (putting the all the second application code to main application) and defining them as one function. Thanks for helping me.Thanks for all :D – imxitiz Jul 27 '20 at 16:57
  • That is the minimal way of doing it – Delrius Euphoria Jul 27 '20 at 17:08
  • Do any of us have better idea to do it? Without using classes!!!!! – imxitiz Jul 27 '20 at 17:13

1 Answers1

0

You can use Toplevel() to create a second window

Try this code:

from tkinter import *
root=Tk()
def ope():
    root1 = Toplevel()
    Label1 = Label(root1,text="Hey welcome in second app")
    Label1.pack()
    root1.mainloop()
Open_1=Button(root,text="Open",command=ope)
Open_1.pack()
root.mainloop()
ThisIsMatin
  • 390
  • 1
  • 12