【发布时间】:2017-12-04 01:01:40
【问题描述】:
如果我在 mainloop() 上方注释掉我的代码的最后 4 行,我的 Tkinter GUI 可以正常工作。我想添加图像。如果我包含了这些行,那么代码运行时不会出现任何错误,但不会显示 GUI。添加图像可能会出现什么问题(我尝试了所有 .jpg、.png、.gif)?
import Tkinter as tk
from PIL import ImageTk, Image
def show_answer():
Ans = (float(num1.get())**2)-(((float(num1.get()) - float(num2.get()))**2)/2)
blank.insert(0, Ans)
main = tk.Tk()
tk.Label(main, text = "Enter Num 1:").grid(row=0)
tk.Label(main, text = "Enter Num 2:").grid(row=1)
tk.Label(main, text = "The Area of pseodo square:").grid(row=2)
num1 = tk.Entry(main)
num2 = tk.Entry(main)
blank = tk.Entry(main)
num1.grid(row=0, column=1)
num2.grid(row=1, column=1)
blank.grid(row=2, column=1)
tk.Button(main, text='Quit', command=main.destroy).grid(row=4, column=0, pady=4)
tk.Button(main, text='Calculate', command=show_answer).grid(row=4, column=1, pady=4)
##img = "download.png"
##photo = ImageTk.PhotoImage(Image.open(img))
##panel = tk.Label(main, image = photo)
##panel.pack(side = "bottom", fill = "both", expand = "yes")
main.mainloop()
【问题讨论】:
-
我认为您的设置有问题。这在我的机器上给出了一个非常描述性的错误: _tkinter.TclError: cannot use geometry manager pack inside 。已经有网格管理的奴隶
标签: python python-2.7 user-interface tkinter