如何在新窗口中从.py文件调用另一个python脚本

2024-06-16 12:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个问题,我想单击全屏应用程序上的按钮

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

它所做的是显示test2接口,但TestOne停止响应。我想要的是test2将显示在上面,两者都将响应,并且是不同的窗口

我英语不好,如果我有问题,很抱歉


Tags: fromimporttitleostkinterbuttonroottk
1条回答
网友
1楼 · 发布于 2024-06-16 12:24:35

如果您可以使用一个“主”窗口来跟踪其他窗口,那么您可以执行以下操作:

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

相关问题 更多 >