我试图学习使用python TTK,我在向按钮添加简单的小图像时遇到错误,可以是与程序在同一文件中的任何图像。这是代码:
# from PIL import ImageTk, Image
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
window = Tk()
window.wm_iconbitmap('icon.ico')
window.geometry('200x200')
user = ttk.Label(window, text='Username').pack()
userEnt = ttk.Entry(window).pack()
passW = ttk.Label(window, text='Password').pack()
passEnt = ttk.Entry(window).pack()
logButton = ttk.Button(window, text='Login').pack()
myImg = PhotoImage(file='C:\\Users\edwin\Desktop\Python\Tkinter\logo.png')
logButton.config(image=myImg, compound=RIGHT)
window.mainloop()
这是我收到的错误,我不知道为什么!
文件" C:/Users/edwin/Desktop/Python/Tkinter/trial.py" ;, 第18行, 在 logButton.config(image = myImg,compound = RIGHT) AttributeError:' NoneType'对象没有属性 ' config'
答案 0 :(得分:0)
您要将值分配给pack()的输出,而不是类实例化的输出。你还需要另一条线:
# from PIL import ImageTk, Image
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
window = Tk()
window.wm_iconbitmap('icon.ico')
window.geometry('200x200')
user = ttk.Label(window, text='Username')
user.pack()
userEnt = ttk.Entry(window)
userEnt.pack()
# so on and so forth