Python Tkinter代码-具有导入图像按钮的框架

时间:2019-03-05 12:32:40

标签: python tkinter

当前,当尝试将图像导入为按钮时,程序将打开一个空白窗口。在不尝试将图像导入为按钮的情况下,代码可以正常工作。任何人都可以帮助修复此代码或为我指明正确的方向吗?

from tkinter import*
root = Tk()

class app(Tk):

def __init__(self, *args, **kwargs):

Tk.__init__(self, *args, **kwargs)
    container = 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, SignIn, SignUp):

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

def __init__(self, parent, controller):
    Frame.__init__(self, parent)
    label = Label(self, text="Start Page")
    label.pack(pady=10,padx=10)

    img1 = PhotoImage(file="signinbtn.png")
    button = Button(self, image=img1,
                        command=lambda: controller.show_frame(SignIn))
    button.pack()

    img2 = PhotoImage(file="signupbtn.png")
    button2 = Button(self, image=img2, text="Sign Up",
                        command=lambda: controller.show_frame(SignUp))
    button2.pack()


class SignIn(Frame):

def __init__(self, parent, controller):
    Frame.__init__(self, parent)
    label = Label(self, text="Sign In")
    label.pack(pady=10,padx=10)

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



class SignUp(Frame):

def __init__(self, parent, controller):
    Frame.__init__(self, parent)
    label = Label(self, text="Sign Up")
    label.pack(pady=10,padx=10)

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



   root.mainloop()

1 个答案:

答案 0 :(得分:0)

空窗口由第二行代码创建:

root = Tk()

除此行外,您还具有类定义。由于您从未实例化任何类,因此不会运行任何类中的代码。