0

I ran into a problem where I want to click a button on a fullscreen app.

test1

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os

root = Tk()
root.title('Gamesim')
root.geometry('500x400')

def cmdopen():
    os.system('C:\Users\User\Desktop\test2.py')


btn = Button(text='test', command=cmdopen)
btn.pack()

root.mainloop()

test2

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os


root = Tk()
root.title('Gamesim')
root.geometry('1870x1080')
root.attributes("-topmost", True)


btn = Button(text='test2')
btn.pack()

root.mainloop()

What it does it displays the test2 interface, but test one stops responding. What I want is that the test2 will apear above and both will respond and are diffrent windows.

Im bad in english so sorry if I have some problems.

marksh16
  • 3
  • 3
  • Instead of `os.system('C:\Users\User\Desktop\example.py')` use a simple import. It should fix your problems – TheLizzard May 07 '21 at 18:06
  • Consider if this solution fits your needs: https://stackoverflow.com/questions/48817749/using-python-how-do-you-call-a-tkinter-gui-from-another-gui – Levente Simofi May 07 '21 at 18:18
  • If not, you need to create an other process or thread, where you call the other app like you did with os.system(). In your case the second app's loop is blocking the first app. – Levente Simofi May 07 '21 at 18:20
  • If needed I'll help you with processes. – Levente Simofi May 07 '21 at 18:23
  • @TheLizzard I think only one root window can exist inside an app (and only one mainloop). – Levente Simofi May 07 '21 at 18:25
  • @LeventeSimofi The second `.mainloop()` will also update the first window. At least that is what I think. It's strange but look at [this](https://stackoverflow.com/q/67007447/11106801). You can have as many `tk.Tk()` as you want but you will get a lot of bugs if you aren't really careful. That is why people advise to only use 1 `tk.Tk()` and then use as many `tk.Toplevel()`s as you need. – TheLizzard May 07 '21 at 18:42
  • @TheLizzard It is interesting! I did not fully understand it, but I think having only one of those is a good practice. Thank you for correcting. – Levente Simofi May 07 '21 at 18:54
  • @LeventeSimofi Most of the times: Yes. But if you are really careful you can have projects with multiple instances of `tk.Tk` without any problems. I prefer using multiple instance of `tk.Tk` because When I started learning `tkinter` no one told me that `tk.Toplevel` existed so I am used to dealing with `tkinter`'s quirks – TheLizzard May 07 '21 at 18:57

1 Answers1

0

If you're okay with having one "master" window that keeps track of the other windows, then you can do something like this:

from tkinter import *
from tkinter.ttk import *
from functools import partial


class subWindow(Toplevel):
    def __init__(self, master=None):
        super().__init__(master=master)

def createSubwindow(master):
    """Creates a subWindow of 'master' and sets it's options"""
    subWin = subWindow(master)
    subWin.title('SubWindow')
    subWin.geometry('500x400')
    subWin.attributes("-topmost", True)
    btn = Button(subWin, text='Button Inside of SubWindow')
    btn.pack()

# Creating the master-window
root = Tk()
root.title('MasterWindow')
root.geometry('500x400')

# Creates a partial of the createSubwindow, so that we can execute it easier in the button.
subWinPartial = partial(createSubwindow, root)

# Installs the button, with the partial function as a command.
btn = Button(root, text='Create Sub Window', command=subWinPartial)
btn.pack()

# Runs the mainloop, that handles all the windows.
root.mainloop()
Hampus Larsson
  • 3,050
  • 2
  • 14
  • 20
  • 1
    I think OP is trying to modularise their code. Also it would be nice if you can follow PEP 8's naming convention. – TheLizzard May 07 '21 at 19:28
  • @TheLizzard You can "modularise" his code, using the same thought-process that I am using in my code. – Hampus Larsson May 07 '21 at 19:30
  • Can you please add a footnote that just says that the `createSubwindow` can be defined in another file and imported using `from import createSubwindow`? I am not 100% sure if OP knows how imports even work given their code. They used `os.system('C:\Users\User\Desktop\example.py')` instead of `import example` which suggests they are new to python – TheLizzard May 07 '21 at 19:33
  • I tried to use import but it wont work unless I use OS to insert the path. The effect will be the same. – marksh16 May 08 '21 at 07:58
  • The other file is actually in another folder unlike in my example. – marksh16 May 08 '21 at 08:00