如何解决python和tkinter中的AttributeError? [英] how to solve AttributeError in python and tkinter?

查看:42
本文介绍了如何解决python和tkinter中的AttributeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本框中显示值,但出现此错误:

I want to display the values in the text boxes, but i'm getting this error:

blue.set(B_mean1)
AttributeError: 'numpy.ndarray' object has no attribute 'set'

我的代码是:

from Tkinter import Tk, Frame, BOTH
from Tkinter import *
import cv2
from collections import *
from CBIR import *
from experiment import *
from scipy.spatial import distance
import Tkinter,tkFileDialog
from PIL import Image, ImageTk

class Example(Frame):
   def __init__(self, parent):
      Frame.__init__(self, parent,background="light grey")            
      self.parent = parent        
      self.initUI()

  def initUI(self):    
      self.parent.title("PISE")
      self.pack(fill=BOTH, expand=1)

def open():
   path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
   custName.set(path)
   im = Image.open(path)
   tkimage = ImageTk.PhotoImage(im)
   myvar=Label(root,image = tkimage)
   myvar.image = tkimage
   myvar.pack()
   myvar.place(x = 100, y = 100) 
   graylist1 = list()
   resizelist1 = list()
   eq_graylist1 = list()
   cont_list1 = list()
   ene_list1 = list()
   homo_list1 = list()
   cor_list1 = list()
   B_mean1 = list()
   G_mean1 = list()
   R_mean1 = list()
   dis_list1 = list()

   imge = cv2.imread(path)
   arr = array(imge)
   g_img = cv2.imread(path,0)
   gray_re_img = cv2.resize(g_img,(256,256))
   graylist1.append(gray_re_img)

   equ = cv2.equalizeHist(gray_re_img)
   eq_graylist1.append(equ)

   re_img = cv2.resize(imge,(256,256))
   resizelist1.append(re_img)

   blue, green, red = cv2.split(re_img)
   total = re_img.size
   B = sum(blue) / total
   G = sum(green) / total
   R = sum(red) / total
   B_mean1.append(B)
   G_mean1.append(G)
   R_mean1.append(R)

   im = skimage.io.imread(path, as_grey=True)
   im = skimage.img_as_ubyte(im)
   im /= 32
   g = skimage.feature.greycomatrix(im, [1], [0], levels=8, symmetric=False, normed=True)
   cont = skimage.feature.greycoprops(g, 'contrast')[0][0]
   cont_list1.append(cont)
   ene = skimage.feature.greycoprops(g, 'energy')[0][0]
   ene_list1.append(ene)
   homo = skimage.feature.greycoprops(g, 'homogeneity')[0][0]
   homo_list1.append(homo)
   cor = skimage.feature.greycoprops(g, 'correlation')[0][0]
   cor_list1.append(cor)
   dis = skimage.feature.greycoprops(g, 'dissimilarity')[0][0]
   dis_list1.append(dis)

   feature_matrix_ip = zip( B_mean1 , G_mean1 , R_mean1, cont_list1 , ene_list1 , homo_list1 , cor_list1 , dis_list1)
   blue.set(B_mean1)

root = Tk()
root.geometry("1105x605+300+300")
app = Example(root)

label = Label(app, text='Python Image Search', fg = 'black',font = 'PoorRichard 24')
label.pack()
label.place(y = 5, x = 0)

img = Image.open('logo.png')
bg_img = ImageTk.PhotoImage(img)

label1 = Label(app, image = bg_img)
label1.place(y = 5, x = 1225)

custName = StringVar(None)
yourName = Entry(app, textvariable=custName)
yourName.grid(column=0,row=0,sticky='EW')
 yourName.update()
yourName.focus_set()
yourName.pack(padx = 20, pady = 20,anchor='n')
yourName.place(y = 60, x = 100, width = 525, height = 25)

blue_label = Label(app,text = 'Blue Mean')
blue_label.place(x = 850,y = 140)
blue = IntVar()
blue_text = Entry(app,textvariable = blue)
blue_text.place(x = 1000,y = 140)

button = Button(app, text='Select an Image',command = open)
button.pack(padx = 1, pady = 1,anchor='ne')
button.place( x = 650, y = 60)

root.mainloop()  

我只想知道如何将值显示到文本框中.欢迎提出任何建议.

All I want to know is how to display the values into the textbox. Any suggestions are welcome.

提前致谢!

推荐答案

您的问题是您将变量名称 blue 用于两种不同的事情.一方面它是一个 numpy 数组,另一方面它是一个 IntVar.当您调用 blue.set(...) 时,您是在 blue 引用一个 numpy 数组的地方执行此操作,因此错误消息 'numpy.ndarray' 对象没有属性 'set'

Your problem is that you are using the variable name blue for two different things. At one point it's a numpy array and at another it is an IntVar. When you call blue.set(...), you are doing that at the point where blue references a numpy array, hence the error message 'numpy.ndarray' object has no attribute 'set'

尝试将 IntVar 的名称更改为其他名称,例如 blue_var,并确保在任何地方都进行了更改.

Try changing the name of your IntVar to something else, such as blue_var, and make sure you change it everywhere.

这篇关于如何解决python和tkinter中的AttributeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆