我一直在试图让我的tkinter子窗口中的图像,当我运行此代码时,我收到没有错误,但我只在子窗口中获得一系列数字。以下是一段我的代码:图片将不会出现在我的tkinter文本部件
from tkinter import *
top = Tk('Outfit Randomizer')
#gender entry
L1 = Label(top, text = 'What is your gender? Type 1 for male and 2 for female')
L1.grid(row = 0, column = 0)
g = StringVar()
E1 = Entry(top, bd = 5, textvariable = g)
E1.grid(row = 0, column = 1)
#submit function
def submit():
g.get()
gender = g.get()
if gender == '1':
root = Toplevel()
T = Text(root, height = 30, width = 90)
T.pack()
mhbs = random.choice([ms1, ms2])
if mhbs == ms1:
photo = PhotoImage(file = "m.GIF")
label = Label(image = photo)
label.image = photo
label.pack()
if mhbs == ms2:
photo = PhotoImage(file = "m.GIF")
label = Label(image = photo)
label.image = photo
label.pack()
T.insert(END, T)
#submit button
MyButton1 = Button(top, text = 'Submit', width = 10, command = submit)
MyButton1.grid(row = 3, column = 1)
top.mainloop()
MS1和MS2在我的代码的不同部分定义,我创建一个男衬衫类,但我觉得这是不必要的这个问题。请让我知道我能做些什么来让我的形象转移到新窗口!
请海h这个网站关于tk ter图像不显示。必须有至少100个相关的问题 –
@BryanOakley我搜索了这个网站以及其他许多人,并有相关的问题,但我还没有找到一个答案,在我的程序 – sammy
@sammy该图像不会出现,因为该标签不包装。看看错误消息。如果你想要其他人修复你的代码,请发布一个[mcve],他们可以真正运行,看看会发生什么。 – Goyo