Python tkinter多个窗口

问题描述 投票:0回答:1

我可以分别运行两个文件。主文件是prj.py当我在prj.py中导入cfg以运行cfg.py时,出现NameError: name 'root' is not defined.试图在必要时尝试访问cfg.py。如果需要cfg.py,请销毁prj.py,然后继续/运行cfg.py

prj.py

import cfg
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.GetChk()        
    def GetChk(self):
        print ('PRJ')
        opn_cfg = tk.Button(root, text='CFG', width=10, command=self.GetCfg)
        opn_cfg.place(x=50, y=50)
    def GetCfg(self):
        print ('prj')
        root.destroy()
        cfg.Configuration()

if __name__ == "__main__":   
    root = tk.Tk()
    root.title('PRJ')
    root.geometry('500x250')
    root.resizable(0,0)
    app = Application(root)
    root.mainloop()

cfg.py

import tkinter as tk

class Configuration(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self)
        self.GetChk()
    def GetChk(self):
        print('CFG')
        opn_prj = tk.Button(root, text='OUT', width=10, command=self.GetPrj)
        opn_prj.place(x=50, y=50)
    def GetPrj(self):
        print('cfg')

if __name__ == "__main__":   
    root = tk.Tk()
    root.title('CFG')
    root.geometry('250x125')
    root.resizable(0,0)
    app = Configuration(root)
    root.mainloop()
python tkinter
1个回答
0
投票

代码

if __name__ == "__main__":

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

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

但是这会带来其他问题。现在root是局部变量,因此它在类内部是不可访问的,您不能在Button()中使用它。但是您将root用作参数app = Configuration(root),因此如果使用

tk.Frame().__init__(super, *args, **kwargs)

或更短(Python 3中为首选)

super().__init__(*args, **kwargs)

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

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

prj.py

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

BTW:我基于lower_case_names将函数名称更改为PEP 8 -- Style Guide for Python Code

BTW:如果您打算销毁Configuration并再次创建Application,那么这样做会遇到很大的问题-因此,这不是一个好主意。您应该替换一个窗口中的内容。或将配置创建为Toplevel并隐藏Application而不是销毁它。

© www.soinside.com 2019 - 2024. All rights reserved.