向Tkin中的按钮添加简单的小图像时出错

2024-10-08 18:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习使用pythontttk,我在向按钮添加一个简单的小图像时不断出错,可以是同一文件中的任何图像程序。代码如下:

# 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()

这是我收到的错误,我不知道为什么!在

File "C:/Users/edwin/Desktop/Python/Tkinter/trial.py", line 18, in logButton.config(image=myImg, compound=RIGHT) AttributeError: 'NoneType' object has no attribute'config'


Tags: textfrom图像imageimportconfigpiltkinter
1条回答
网友
1楼 · 发布于 2024-10-08 18:28:47

将值赋给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

相关问题 更多 >

    热门问题