python - 如何在新窗口中从 .py 文件调用另一个 python 脚本
问题描述
我遇到了一个问题,我想单击全屏应用程序上的按钮。
测试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()
测试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()
它的作用是显示 test2 界面,但 test 1 停止响应。我想要的是 test2 会出现在上面,并且两者都会响应并且是不同的窗口。
我的英语不好,如果我有一些问题很抱歉。
解决方案
如果您可以拥有一个跟踪其他窗口的“主”窗口,那么您可以执行以下操作:
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()
推荐阅读
- angular - Angular 6 Material - Await until Mat Dialog is closed
- powershell - 签入powershell权限字符串是否有效
- android - Mapbox - Reverse Geocoding - multiple language response
- javascript - 正则表达式获取除括号内的每个非特殊单词
- angularjs - 使用 2 种方法 POST 和 PUT 发出 js 请求
- asp.net - host https website using IIS across local area network
- eloquent - 在 null 上对成员函数 connection() 的代码接收调用
- c# - 基于 EndTime 属性刷新 WPF ListView
- node.js - How allow my app to POST a cloudfunction in firebase that makes another POST?
- javascript - 在构建时明确导入路径