Python Tkinter,方法中的tk.Frame容器小部件错误

时间:2015-05-28 10:10:57

标签: python tkinter

我正在编写一个使用Tkinter打开框架的GUI代码。我提到了各种网站。现在在测试时我面临着问题。例如:

  • 在第1帧中,我选择MainController按钮。
  • 第二帧中的
  • MC_CONFIG按钮。
  • 在第3帧中,我设置了XML PATH,然后点击了MC SYSTEM.xml按钮

如果我转到Back to Home按钮并按照相同的步骤,MC_CONFIG按钮被禁用(即我无法继续)。

如果我评论(删除)此行(126)

tk.Frame.__init__(self)

在班级def nacxml(self):的方法MC_CONFIG中,它运作正常。

下面的内容只是我主要代码问题的一部分。 请指导我。

import Tkinter as tk
import xml.dom.minidom
from Tkinter import *
import tkMessageBox 
from array import *
import tkFileDialog
import os
LARGE_FONT= ("Verdana", 12)

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Switch Installer window")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        #for F in (StartPage, PageOne, PageTwo):
        for F in (StartPage, MainController,xmlpath,MC_CONFIG):
            frame = F(container,self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.modules_label = ['MAINCONTROLLER']
        self.modules_function = [MainController]
        self.modules_label_index = len(self.modules_label)
        self.modules_function_index = len(self.modules_function)
        print("self.modules_label_index = %s" %self.modules_label_index)

        label = Label(self, text="SWITCH INSTALLER", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        #button = Button(self, text="Visit Page 1",

        button3 = Button(self, text="SELECT",
                            command=lambda: controller.show_frame(MainController))
        button3.pack()

        label3 = Label(self, text="MainController", font = LARGE_FONT)
        label3.place(x= 50, y=100+10)
        button8 = Button(self, text="Quit", command=self.quit)
        button8.pack() 

class xmlpath(tk.Frame):
#xfilename="+++"
    def __init__(self, parent, controller):
        self.xfilename="srinivasan"
        tk.Frame.__init__(self, parent)
        label = Label(self, text="Page One!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button1 = Button(self, text="XML PATH",
                            command=self.newxmlpath)
        button1.pack()

    def newxmlpath(self,*args):
        # ObjNAC= NacHandler()
        self.filename = tkFileDialog.askopenfilename()
        print(self.filename)
        #ObjNAC.temp_method(self,self.filename)
        return self.filename

class MainController(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = Label(self, text="|--Frame1 MainController --|", font = LARGE_FONT)
        label.pack(pady=10,padx=10)

        mc_button1 = Button(self, text="MC_CONFIG", command = lambda: controller.show_frame(MC_CONFIG))
        mc_button1.pack()

        mc_button2 = Button(self, text="MENU HOME", command = lambda: controller.show_frame(StartPage))
        mc_button2.pack()

        self.pack (fill = BOTH, expand = 1)     

class MC_CONFIG(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        print "Inside MC_CONFIG"

        self.database = []
        # set root as parent
        self.parent = parent

        label1 = Label(self, text="|------------Frame2--MainController---------------|", font=LARGE_FONT)
        label1.pack(pady = 10,padx = 10)

        label2 = Label(self, text="Edit SYSTEM.xml File", font=LARGE_FONT)
        label2.pack(pady = 10,padx = 10)

        button1 = Button(self, text="XML PATH",
                            command=self.newxmlpath)
        button1.pack(pady = 10,padx = 10)

        button2 = Button(self, text = "MC SYSTEM.xml", command = self.nacxml)
        button2.pack(pady = 10,padx = 10)

        button3 = Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button3.pack(pady = 10,padx = 10)

    def newxmlpath(self, *args):
        self.filename = tkFileDialog.askopenfilename()
        print(self.filename)
        return self.filename

    def nacxml(self):
        tk.Frame.__init__(self)
        print "===Inside Nacxml1==="

app = SeaofBTCapp()
app.geometry ("640x480+300+300")
app.mainloop()    

2 个答案:

答案 0 :(得分:0)

问题在于:

def nacxml(self):
    tk.Frame.__init__(self)

您应该只从子类的构造函数中调用超类构造函数。在其他地方这样做肯定不会做你认为正在做的事情。

答案 1 :(得分:0)

最后代码按预期工作 1.删​​除 tk.Frame .__ init __(self),如Bryan Oakley所示

 def nacxml(self):
    tk.Frame.__init__(self)

2.Controller不是在self中初始化。但是它存在于 def __init __(自我,父级,控制器): .Hence中添加 MC_CONFIG类中的 self.controller = controller

class MC_CONFIG(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    print "Inside MC_CONFIG"

因此可以在下面的方法中用作self.controller.show_frame(StartPage)

def nacxml(self):
    tk.Frame.__init__(self)
    print "===Inside Nacxml1==="
    # xml read,edit,widget code will be added here
    self.controller.show_frame(StartPage)