Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 使用tkinter获取条目的值_Python 2.7_User Interface_Tkinter - Fatal编程技术网

Python 2.7 使用tkinter获取条目的值

Python 2.7 使用tkinter获取条目的值,python-2.7,user-interface,tkinter,Python 2.7,User Interface,Tkinter,我想知道如何使用Entry显示值?我的意思是,例如,如果我有一个函数,如: def open(): path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')]) blue, green, red = cv2.split(path) total = path.size B = sum(blue) / total G = sum(green) / total R = sum(r

我想知道如何使用Entry显示值?我的意思是,例如,如果我有一个函数,如:

def open():
    path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
    blue, green, red = cv2.split(path)
    total = path.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)

    blue.set(B_mean1)
    green.set(G_mean1)
    red.set(R_mean1)

root = Tk()

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

green_label = Label(app,text = 'Green Mean')
green_label.place(x = 850,y = 170)
green = IntVar(None)
green_text = Entry(app,textvariable = green)
green_text.place(x = 1000,y = 170)

red_label = Label(app,text = 'Red Mean')
red_label.place(x = 850,y = 200)
red = IntVar(None)
red_text = Entry(app,textvariable = red)
red_text.place(x = 1000,y = 200) 

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()  
我已经指定了所有必要的导入和列表变量。输入字段中仍然没有显示该值

比如,如果我得到如下值: 蓝色平均值=37, 绿色平均值=36, 红色平均值=41

它将在控制台中打印,但不会在窗口中打印。我怎样才能做到这一点

欢迎提出任何建议!
谢谢你的支持

首先,我建议您不要使用
place
方法,而是使用
grid
方法。它的速度要快得多,并且允许您以更有序的方式进行操作,尤其是在这个应用程序中。请注意,还有其他几何图形管理器,如
pack
,在其他情况下也很好。无论如何,您的GUI:

root = Tk()

blue_label = Label(root, text="Blue mean:")
blue_label.grid(row=1, column=1, sticky="w")
blue_entry = Entry(root)
blue_entry.grid(row=1, column=2)

green_label = Label(root, text="Green mean:")
green_label.grid(row=2, column=1, sticky="w")
green_entry = Entry(root)
green_entry.grid(row=2, column=2)

red_label = Label(root, text="Red mean:")
red_label.grid(row=3, column=1, sticky="w")
red_entry = Entry(root)
red_entry.grid(row=3, column=2)

root.mainloop()
这使您的GUI显示得很好,并且没有手动几何图形。请注意,sticky只设置文本对齐方式(“w”表示向西,因此文本向左对齐)

现在,要真正显示文本,只需执行以下操作:

blue_entry.insert(0, "blue-mean-value")
green_entry.insert(0, "green-mean-value")
red_entry.insert(0, "red-mean-value")
如果要覆盖已有的任何文本,请执行以下操作:

blue_entry.delete(0, END) # And green_entry and red_entry
StringVar()
而不是
IntVar()