Tkinter程序上没有显示背景图像?

时间:2019-02-26 19:59:49

标签: python tkinter

互联网人!我是TKinter GUI开发的新手,所以下面发生的许多过程都是我今天刚刚学到的。 尽管我尝试添加的按钮和其他小部件都没有显示,但背景图片(bg.jpg)没有显示。有什么帮助吗?预先感谢。

import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        img = ImageTk.PhotoImage(Image.open("bg.jpg"))

        Width = img.width() // 2
        Height = img.height() // 2

        parent.geometry("{}x{}".format(Width, Height))
        parent.minsize(Width, Height)          # minsize
        parent.maxsize(Width * 2, Height * 2)  # maxsize

        canv = tk.Canvas(parent, width=img.width() * 1.5, height=img.width() * 1.5, bg='white')
        canv.grid(row=0, column=0)
        canv.create_image(0, 0, anchor=tk.NW, image=img)

        class quitButton(tk.Button):
            def __init__(self, parent):
                tk.Button.__init__(self, parent)
                self['text'] = 'exit'
                self['command'] = parent.destroy
                self['bg'] = '#ff4444'
                self.place(relx=0.9, rely=0.9, anchor=tk.CENTER)
        quitButton(parent)

        randomButton = tk.Button(parent, text="text1", bg='#63d8c7')
        randomButton.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root)
    root.mainloop()

0 个答案:

没有答案