问题描述
限时送ChatGPT账号..我制作了 ttk 树视图,您可以在其中手动添加值.(我不打算发布整个代码,因为它太大了).我还制作了一个函数,该函数应该计算两列的乘法并将结果放在第三列中.我将两个事件绑定到它,使用 ENTER 键进行验证(
)并在您单击其他单元格时进行验证(
).出于某种原因,该程序仅在您按 ENTER 键时才起作用,而它不适用于 FocusOut
.它没有显示任何错误,只是不起作用.你知道是什么问题吗?
I made ttk treeview in which you can manually add values.
(Im not going to post the whole code because its to big).
I also made a function that should calculate multiplication of two columns and put the result in third column. I bind two events to it , validate with ENTER key (<Return>
) and validate when you click on other cell (<FocusOut>
).
From some reason, the program works only when you press ENTER key, and it does not work with FocusOut
. It does not show any error, it just does not work. Do you know whats the problem?
def TotalCost(event):
try:
SelectedRow = NewTree.selection()[0]
Quantity=round(float(NewTree.item(SelectedRow,"values")[3]),2)
UnitCost=round(float(NewTree.item(SelectedRow,"values")[4]),2)
TotalCost=float(round(Quantity*UnitCost,2))
NewTree.set(SelectedRow, '#6', TotalCost)
except IndexError:
sys.exit()
pass
except ValueError:
Error=messagebox.showinfo("Error!","Please enter values for Planned Costs or Real Costs.")
sys.exit() #za resavalje greske
pass
NewTree.bind('<Return>', TotalCost) # validate with Enter
NewTree.bind('<FocusOut>', TotalCost) # validate when you click on other cell
推荐答案
这里是一个代码示例,用于说明没有任何按钮的绑定功能和调用功能.我用过 python 2.7
Here is an example of code to illustrate the binding functions and invoking function without any button. i have used python 2.7
import Tkinter as tk
main=tk.Tk()
var=tk.StringVar()
def rockon(event):
try:
var.set(int(e1.get())+int(e2.get()))
except:
pass
e1=tk.Entry(main)
e1.place(x=10,y=10)
e1.insert(tk.END,0)
e1.bind('<FocusOut>',rockon) #either u press tab
e1.bind('<Leave>',rockon) #either u move out of the widget
e1.bind('<Return>',rockon) #or u press enter key, it goes into the function "rockon"
e2=tk.Entry(main)
e2.place(x=10,y=50)
e2.insert(tk.END,0)
e2.bind('<FocusOut>',rockon)
e2.bind('<Leave>',rockon)
e2.bind('<Return>',rockon)
label=tk.Label(main,textvariable=var) #we print the total of two entries in label without pressing any button
label.place(x=10,y=100)
main.mainloop()
这篇关于绑定到树视图的函数不起作用 Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论