_tkinter.TclError:不好的一面:必须是顶部,底部,左侧或右侧

时间:2016-03-23 11:38:42

标签: python tkinter

我定义了一个使用以下代码创建标签的方法

from tkinter import * 

class LoginFrame(Frame):
    def __init__(self, parent):
        super(LoginFrame, self).__init__()

        self.parent = parent        
        self.initUI()

    # initialize the login screen UI  
    def initUI(self):
        self.parent.title("Login Screen")

        # Set up login frame properties 
        self.parent.title("Login Screen")

        # create a background image 
        photo_bg = PhotoImage(file="building.gif")          
        building = self.make_label(self.parent, image=photo_bg)

        # creating instruction label, where the error occurs
        inst_lbl = self.make_label(self.parent, "Please login to continue")       


    def make_label(parent, caption=NONE, side=TOP, **options):
        label = Label(parent, text=caption, **options)

        if side is not TOP:
            label.pack(side=side)
        else:
            label.pack()

        return label


def main():
    top = Tk()    
    app = LoginFrame(top)
    top.mainloop()


if __name__ == '__main__':
    main()

尝试运行代码时出错了,

_tkinter.TclError: bad side "Please login to continue": must be top, bottom, left, or right

我想知道如何解决这个问题?使用make_label创建背景图像时没有错误。

1 个答案:

答案 0 :(得分:1)

您对make_label的定义缺少self参数。您需要将其定义为:

def make_label(self, parent, ...):