简体   繁体   中英

How to call another python script from a .py file in a new window

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM