Tkinter button命令用于切换带帧的类

时间:2015-07-31 16:09:41

标签: python user-interface tkinter

我是Tkinter的新手,我在按钮命令切换到其他课程时遇到了一些麻烦。

import tkinter as tk


class MainApplication(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        button = tk.Button(self, text="Login", command=self.gotoMainMenu)
        button.pack()

    def gotoMainMenu(self):
        root2=tk.Toplevel(self)
        myGUI=MainMenu(root2)

    def finish(self):
        self.parent.destroy()

class MainMenu(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        button = tk.Button(self, text="Visit Page 1")
        button.pack()

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

这是我正在处理的当前代码。我试过看过例子,但我似乎无法弄明白。我目前收到属性错误。 " AttributeError:MainApplication实例没有属性' gotoMainMenu'。"此外,非常感谢您找到的任何错误或建议您。

1 个答案:

答案 0 :(得分:2)

您根本忘记打包// Inits, can also be put into the loader. var extensions = ['.jpg', '.JPG', '.png', '.PNG']; var imgUrl = "some/path/"; var loader = function(fileName, success, fail) { // Start at first image type. var index = 0; // Create a img for loading. var $image = $('<img>'); // Add success and fail behavior to the image. $image .load(function() { // Do some notification or.... console.log('success', $image.attr('src')); // If you give a success callback, call it // Or you can write the logic here. if ($.isFunction(success)) { success(); } }) .error(function() { console.log('fail', $image.attr('src')); // Try to load next image tryLoad(index); }); // The function for attempts. var tryLoad = function() { // When all attemps tried. if (index === extensions.length) { alert('Boom, all failed'); // Do some fallbacks.... // $image.remove(); // If you give a fail callback, call it // Or you can write the logic here. if ($.isFunction(fail)) { fail(); } return; } // Create url to load. var tmpImgUrl = imgURL + fileName + extensions[index]; console.log('test', tmpImgUrl); $image.attr('src', tmpImgUrl); ++index; }; // Start the first attempt. tryLoad(); // return jquery wrapper of image. It'll have image only if any of the test is passed. return $image; }; 框架。将以下行添加到MainMenu类构造函数的末尾:

MainMenu