绑定到树视图的函数不起作用 Tkinter

编程入门 行业动态 更新时间:2024-10-25 04:22:55
本文介绍了绑定到树视图的函数不起作用 Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 06:33:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   绑定   函数   不起作用   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!