简体   繁体   English

如何从新 window 中的 .py 文件调用另一个 python 脚本

[英]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测试1

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测试2

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.它的作用是显示 test2 界面,但 test 1 停止响应。 What I want is that the test2 will apear above and both will respond and are diffrent windows.我想要的是test2会出现在上面并且两者都会响应并且是不同的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:如果您对拥有一个跟踪另一个 windows 的“主”window 感到满意,那么您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个.py文件运行python脚本并在当前窗口中显示 - How to run python script from another .py file and show in current window 如何从python代码在新的shell窗口上调用python脚本? - How to call a python script on a new shell window from python code? 如何调用另一个 python 文件,运行它,并将 main.py 替换为 new.py 文件? - How to call another python file, run it, and replace main.py with the new .py file? 如何从python中的另一个.py文件调用函数? - How to call function from another .py file in python? 如何从 another.py 文件中调用模块 - How to call modules from another .py file 如何从another.py调用Python代码? (当我按空格键时 both.py 在同一个文件中) - How to call Python code from another .py? (when I press space bar both .py are in the same file) 如何将另一个 python 脚本 (.py) 导入到主 python 文件中 - How to import another python script (.py) into main python file 使用 os 从另一个 Python 脚本运行 .py 文件 - running a .py file from another Python script using os 来自另一个py文件的maya python调用函数 - maya python call function from another py file 从另一个 python 脚本写入 config.py 文件 - Writing to config.py file from another python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM