本文介绍了tkinter变量跟踪方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试在用户在tkinter输入框中键入内容时激活回调函数.根据 effbot.org

I am trying to activate callback function as the user type in to the tkinter entry box. According to effbot.org,

所以我很累

import Tkinter as tk
window2=tk.Tk()
anf_frame1=tk.Frame(window2,bg='white',relief='groove',bd=0,width=1190,height=175)
anf_frame1.place(x=10,y=50)

def myfunction(*args):
        print 'pass'
stringvar1 = tk.StringVar(anf_frame1)
stringvar1.trace("w", myfunction)
anf_fault_entry=tk.Entry(anf_frame1,width=35,font=('calibri',(14)),bg='white',textvariable=stringvar1)
anf_fault_entry.grid(row=2,column=1,padx=(5,5))

window2.mainloop()

上面的脚本工作正常.但是当我将其复制到主脚本时,它不再打印出"pass".它也没有给我任何错误.

The above script works fine.But when i copy this to my main script, it doesn't print out the 'pass' anymore. It doesn't give me any error either.

我已经确认没有其他变量名 stringvar1 和相同的函数名 myfunction .而且没有错字错误,因为我只是使用了复制粘贴功能.

I have confirmed that there is no other variable name stringvar1 and same function name myfunction. And there is no typo error as i just used copy paste function.

现在我很困惑为什么将其复制到主脚本时不起作用.

I am now puzzled why it doesn't work when i copy it to my main script.

仅供参考,在复制跟踪回调之前和之后,我的主脚本正在按预期方式工作.我的主脚本有一个带有一些标签和输入框的tkinter窗口,它们不应该影响上述操作.是什么原因引起的?我错过了什么吗?

Just for info, my main script is working as it should before i copy the trace callback and after. My main script has tkinter window with a few labels and entry boxes which should not affect the above operation. What is causing the problem? Did i miss something?

---编辑---

def Entrybox_002():
    def myfunction(*args):
            print 'pass'

    window2=tk.Toplevel(root)
    md1.Drag_Window(window2, nf_sizex, nf_sizey, nf_posx, nf_posy, nf_title, nf_titlesize,nf_level)
    ''' New Entry labels & Dropdowns'''
    #Frame to hold labels
    anf_frame1=tk.Frame(window2,bg='white',relief='groove',bd=0,width=1190,height=175)
    anf_frame1.place(x=10,y=50)
    anf_frame2=tk.Frame(window2,bg='#CCF1FF',relief='groove',bd=0,width=700,height=85)
    anf_frame2.place(x=50,y=140)

    label_list=['JCN','Fault','System','Sub-System','Status','Faultcode']
    for i in range (6):
        tk.Label(anf_frame1,text=label_list[i],font=('calibri',(16)),bg='white').grid(row=1,column=i,padx=(40,40),pady=(5,5))

    anf_jcn_number=tk.Label(anf_frame1,text=Calculate_linenumber(),font=('calibri',(16)),bg='white',fg='blue')
    anf_jcn_number.grid(row=2,column=0)


    stringvar1 = tk.StringVar(anf_frame1)
    stringvar1.trace("w", myfunction)
    anf_fault_entry=tk.Entry(anf_frame1,width=35,font=('calibri',(14)),bg='white',textvariable=stringvar1) #<------------------------ENTRY BOX THAT I AM TRYING TO TRACE
    anf_fault_entry.grid(row=2,column=1,padx=(5,5))

    anf_system_menu = md1.MyOptionMenu(anf_frame1, 'Select System', anf_system_choices,Subsytem_display)
    anf_system_menu.grid(row=2,column=2,padx=(5,5))
    (anf_system_menu.var).trace("w",myfunction)

    anf_status_menu = md1.MyOptionMenu(anf_frame1, 'Select Status', anf_status_choices,Subsytem_display)
    anf_status_menu.grid(row=2,column=4,padx=(5,5))
    (anf_status_menu.var).trace("w",myfunction)


    anf_faultcode_menu1 = md1.MyOptionMenu(anf_frame1, 'When fault found?', anf_faultcode_1,Operational_effect)
    anf_faultcode_menu1.grid(row=2,column=5,padx=(5,5))
    (anf_faultcode_menu1.var).trace("w",myfunction)

    anf_date_button=tk.Button(anf_frame2,image=images.adf_date_chooser,text='Date',compound='left',font=('calibri',(12),'italic'),bg='white',command=Create_calendar)
    anf_date_button.grid(row=1,column=0,padx=(15,15),pady=(5,5))

    anf_ownership=tk.Label(anf_frame2,text='Reported by',font=('calibri',(14),'italic'),bg='#CCF1FF')
    anf_ownership.grid(row=1,column=1,padx=(15,15),pady=(5,5))

    anf_date_label=tk.Label(anf_frame2,text=today_date,font=('calibri',(14),'italic'),bg='#CCF1FF',fg='blue')
    anf_date_label.grid(row=2,column=0,padx=(15,15),pady=(5,5))

    anf_button1=tk.Button(window2,text='Submit',relief='groove',bd=1,font=('calibri',(12)),bg='green',padx=10,pady=3,command=Submit_Newfault)
    anf_button1.place(x=1100,y=175)
    anf_button1.config(state='normal',bg='grey')

这是创建我要询问的输入框( anf_fault_entry )的函数.它在其他几个小部件的顶层窗口中.我尝试使用与输入框相同的功能来跟踪其他选项菜单(例如 anf_system_menu anf_subsystem_menu ),但对于这些选项菜单来说似乎工作正常.我不知道发生了什么事.

This is the function that create the entry box (anf_fault_entry) i was asking about. It is on a top level window with other several widgets. I tried to trace the other option menus (for example anf_system_menu and anf_subsystem_menu) using the same function i used for entry box but it seems working fine for those option menu. I have no idea what's going on.

推荐答案

stringvar1 在Entrybox_002函数返回时被垃圾回收(删除).

stringvar1 is garbage collected (deleted) when Entrybox_002 function return.

解决方法:

stringvar1 = window2.stringvar1 = tk.StringVar(value='asdf')

我建议您将Entrybox_002编码为类,将stringvar1编码为实例属性以保持引用.

I recommend you code Entrybox_002 as class, and stringvar1 as instance attribute to keep reference.

这篇关于tkinter变量跟踪方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:23