Python tkinter多窗口

Python tkinter多窗口,python,tkinter,Python,Tkinter,我可以分别运行这两个文件。当我在prj.py中导入cfg以运行cfg.py时,主文件是prj.py。我得到name错误:未定义名称“root”。如果需要,尝试让选项访问cfg.py。如有必要,销毁prj.py继续/运行cfg.py prj.py import tkinter as tk class Configuration(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args,

我可以分别运行这两个文件。当我在prj.py中导入cfg以运行cfg.py时,主文件是prj.py。我得到
name错误:未定义名称“root”。
如果需要,尝试让选项访问cfg.py。如有必要,销毁prj.py继续/运行cfg.py

prj.py

import tkinter as tk

class Configuration(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print('CFG')
        opn_prj = tk.Button(self.master, text='OUT', width=10, command=self.get_prj)
        opn_prj.place(x=50, y=50)

    def get_prj(self):
        print('cfg')

def create_window():
    root = tk.Tk()
    root.title('CFG')
    root.geometry('250x125')
    root.resizable(0, 0)
    app = Configuration(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
import cfg
import tkinter as tk

class Application(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print ('PRJ')
        opn_cfg = tk.Button(self.master, text='CFG', width=10, command=self.get_cfg)
        opn_cfg.place(x=50, y=50)

    def get_cfg(self):
        print ('prj')
        self.master.destroy()
        cfg.create_window()

def create_window():
    root = tk.Tk()
    root.title('PRJ')
    root.geometry('500x250')
    root.resizable(0, 0)
    app = Application(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
导入cfg
将tkinter作为tk导入
类应用程序(tk.Frame):
定义初始化(self,*args,**kwargs):
tk.Frame.\uuuu init\uuuuu(self,*args,**kwargs)
self.GetChk()
def GetChk(自身):
打印('PRJ')
opn_cfg=tk.Button(root,text='cfg',width=10,command=self.GetCfg)
位置(x=50,y=50)
def GetCfg(自我):
打印('prj')
root.destroy()
cfg.Configuration()
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
root.title('PRJ')
根几何体('500x250')
根目录。可调整大小(0,0)
app=应用程序(根)
root.mainloop()
cfg.py

import tkinter as tk

class Configuration(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print('CFG')
        opn_prj = tk.Button(self.master, text='OUT', width=10, command=self.get_prj)
        opn_prj.place(x=50, y=50)

    def get_prj(self):
        print('cfg')

def create_window():
    root = tk.Tk()
    root.title('CFG')
    root.geometry('250x125')
    root.resizable(0, 0)
    app = Configuration(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
import cfg
import tkinter as tk

class Application(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print ('PRJ')
        opn_cfg = tk.Button(self.master, text='CFG', width=10, command=self.get_cfg)
        opn_cfg.place(x=50, y=50)

    def get_cfg(self):
        print ('prj')
        self.master.destroy()
        cfg.create_window()

def create_window():
    root = tk.Tk()
    root.title('PRJ')
    root.geometry('500x250')
    root.resizable(0, 0)
    app = Application(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
将tkinter作为tk导入
类配置(tk.Frame):
定义初始化(self,*args,**kwargs):
tk.Frame.\uuuu init\uuuuu(自)
#self.root=tk.tk()
#self.root.title('CFG')
#self.root.geometry('250x125')
#self.root.reshable(0,0)
self.GetChk()
def GetChk(自身):
print('CFG')#(self.root,text='OUT',width=10,command=self.GetPrj)
opn_prj=tk.Button(root,text='OUT',width=10,command=self.GetPrj)
位置(x=50,y=50)
def GetPrj(自身):
打印('cfg')
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()#如果在init中则注释掉
root.title('CFG')#在init中注释掉
root.geometry('250x125')#如果在init中则注释掉
root.resizeable(0,0)#如果在init中则注释掉
app=配置(根)
root.mainloop()
代码

if __name__ == "__main__":
这是一种特殊的构造,仅在直接运行脚本时执行某些代码,但现在在导入脚本时执行

这是你的主要问题。您必须将此代码放入可以在第二个脚本中手动执行的函数-
cfg.create\u window()

但这也带来了其他问题。现在
root
是局部变量,所以它在类中不可访问,并且不能在
Button()
中使用它。但是您将
root
作为参数
app=Configuration(root)
使用

tk.Frame().__init__(super, *args, **kwargs)
或更短(Python 3中的广告首选)

然后,
Frame
将获得
root
并分配给
self.master
,您可以在
按钮中使用
self.master

cfg.py

import tkinter as tk

class Configuration(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print('CFG')
        opn_prj = tk.Button(self.master, text='OUT', width=10, command=self.get_prj)
        opn_prj.place(x=50, y=50)

    def get_prj(self):
        print('cfg')

def create_window():
    root = tk.Tk()
    root.title('CFG')
    root.geometry('250x125')
    root.resizable(0, 0)
    app = Configuration(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
import cfg
import tkinter as tk

class Application(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print ('PRJ')
        opn_cfg = tk.Button(self.master, text='CFG', width=10, command=self.get_cfg)
        opn_cfg.place(x=50, y=50)

    def get_cfg(self):
        print ('prj')
        self.master.destroy()
        cfg.create_window()

def create_window():
    root = tk.Tk()
    root.title('PRJ')
    root.geometry('500x250')
    root.resizable(0, 0)
    app = Application(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
prj.py

import tkinter as tk

class Configuration(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print('CFG')
        opn_prj = tk.Button(self.master, text='OUT', width=10, command=self.get_prj)
        opn_prj.place(x=50, y=50)

    def get_prj(self):
        print('cfg')

def create_window():
    root = tk.Tk()
    root.title('CFG')
    root.geometry('250x125')
    root.resizable(0, 0)
    app = Configuration(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
import cfg
import tkinter as tk

class Application(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print ('PRJ')
        opn_cfg = tk.Button(self.master, text='CFG', width=10, command=self.get_cfg)
        opn_cfg.place(x=50, y=50)

    def get_cfg(self):
        print ('prj')
        self.master.destroy()
        cfg.create_window()

def create_window():
    root = tk.Tk()
    root.title('PRJ')
    root.geometry('500x250')
    root.resizable(0, 0)
    app = Application(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()

顺便说一句:我根据

顺便说一句:如果您计划销毁
配置
并重新创建
应用程序
,那么您将遇到很大的问题,所以这不是一个好主意。您应该在一个窗口中替换内容。或者将配置创建为
顶级
,并隐藏
应用程序
,而不是销毁它

代码

这是一种特殊的构造,仅在直接运行脚本时执行某些代码,但现在在导入脚本时执行

这是你的主要问题。您必须将此代码放入可以在第二个脚本中手动执行的函数-
cfg.create\u window()

但这也带来了其他问题。现在
root
是局部变量,所以它在类中不可访问,并且不能在
Button()
中使用它。但是您将
root
作为参数
app=Configuration(root)
使用

tk.Frame().__init__(super, *args, **kwargs)
或更短(Python 3中的广告首选)

然后,
Frame
将获得
root
并分配给
self.master
,您可以在
按钮中使用
self.master

cfg.py

import tkinter as tk

class Configuration(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print('CFG')
        opn_prj = tk.Button(self.master, text='OUT', width=10, command=self.get_prj)
        opn_prj.place(x=50, y=50)

    def get_prj(self):
        print('cfg')

def create_window():
    root = tk.Tk()
    root.title('CFG')
    root.geometry('250x125')
    root.resizable(0, 0)
    app = Configuration(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
import cfg
import tkinter as tk

class Application(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print ('PRJ')
        opn_cfg = tk.Button(self.master, text='CFG', width=10, command=self.get_cfg)
        opn_cfg.place(x=50, y=50)

    def get_cfg(self):
        print ('prj')
        self.master.destroy()
        cfg.create_window()

def create_window():
    root = tk.Tk()
    root.title('PRJ')
    root.geometry('500x250')
    root.resizable(0, 0)
    app = Application(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
prj.py

import tkinter as tk

class Configuration(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print('CFG')
        opn_prj = tk.Button(self.master, text='OUT', width=10, command=self.get_prj)
        opn_prj.place(x=50, y=50)

    def get_prj(self):
        print('cfg')

def create_window():
    root = tk.Tk()
    root.title('CFG')
    root.geometry('250x125')
    root.resizable(0, 0)
    app = Configuration(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()
import cfg
import tkinter as tk

class Application(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.get_chk()

    def get_chk(self):
        print ('PRJ')
        opn_cfg = tk.Button(self.master, text='CFG', width=10, command=self.get_cfg)
        opn_cfg.place(x=50, y=50)

    def get_cfg(self):
        print ('prj')
        self.master.destroy()
        cfg.create_window()

def create_window():
    root = tk.Tk()
    root.title('PRJ')
    root.geometry('500x250')
    root.resizable(0, 0)
    app = Application(root)
    root.mainloop()

if __name__ == "__main__":   
    create_window()

顺便说一句:我根据


顺便说一句:如果您计划销毁
配置
并重新创建
应用程序
,那么您将遇到很大的问题,所以这不是一个好主意。您应该在一个窗口中替换内容。或者将配置创建为
顶级
,并隐藏
应用程序
,而不是销毁它

始终将完整的错误消息(从单词“Traceback”开始)作为文本(而不是屏幕截图)进行讨论(不是评论)。还有其他有用的信息。如果要显示两个窗口,则第二个窗口应该是
tk。Toplevel
-
tkinter
应该只运行一个窗口
tk.tk()
和一个
mainloop()
。您应该将它作为参数发送到class-
Configure(second\u window)
并将其保持为ie.
self.window
并使用
按钮(self.window,…)
顺便说一句:如果uuuu name\uuuu==“\uuuu main\uu”,您必须记住
中的代码:
导入
cfg.py
时不会执行
cfg.py
中的
,因此它不会创建secodn
tk.tk()
,也不会设置标题和几何图形。您必须自己创建第二个窗口。您可以在
\uuuuu init\uuuuuu
中设置标题和几何体,现在我看到您的主要问题是
导入
时,如果{uuuuu name\uuuuuuu==“\uuuuuuu main\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。您必须将
中的代码放入某个函数中,例如
创建窗口()
,然后在第二个文件中运行
cfg.create\u window()
始终将完整的错误消息(从单词“Traceback”开始)作为文本(而不是屏幕截图)放入问题中(不是注释)。还有其他有用的信息。如果要显示两个窗口,则第二个窗口应该是
tk。Toplevel
-
tkinter
应该只运行一个窗口
tk.tk()
和一个
mainloop()
。您应该将其作为参数发送到class-
Configure(第二个窗口)
,并将其保持为ie.
self.window