使用班级

时间:2017-07-24 17:24:06

标签: python python-3.x tkinter

我正在尝试为tkinter应用添加背景信息。出于某种原因,当我使用类继承时,我无法使其工作:

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        C = tk.Canvas(self, bg="blue", height=0, width=0)
        filename = tk.PhotoImage(file='bg.png')
        background_label = tk.Label(self, image=filename)
        background_label.place(x=0, y=0, relwidth=1, relheight=1)
        C.pack()

        self.geometry('400x200')

app = App()
app.mainloop()

但如果我这样做:

import tkinter as tk
top = tk.Tk()
C = tk.Canvas(top, bg="blue", height=0, width=0)
filename = tk.PhotoImage(file='bg.png')
background_label = tk.Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()

top.geometry('200x200')
top.mainloop()

它完美无缺。知道我错过了什么吗?

0 个答案:

没有答案