回调和n输入框小部件不起作用Tkinter [英] Callback and n Entry box widgets not functioning Tkinter

查看:124
本文介绍了回调和n输入框小部件不起作用Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我需要返回所有返回的值(但对于每一行单独),但我已经尝试了lambda和失败,我不会在任何地方AGAIN。



我也有回调定义它以前工作,当我点击一个框,它删除灰色写作,所以我可以继续黑色输入。



如果可能我想在回调函数结束时为输入框添加值return,而不是保留numberwritten和callback。这是否可能?



添加的框数等于self.number_boxes,可以是从1到n。



谢谢。



更新的代码:

  self.numbers = [xml(self.number_boxes)中的i的StringVar 
$ b for i in xrange(self.number_boxes):
row =(row_number + add)
column = self.column + i
key =%d /%d %(row,column)
self.clicked =([False] * self.number_boxes)
self.choice_title = Label(self.frame_table,bg = self.mycolour,borderwidth = 0,width = 10)
self.choice_title.grid(row = 1,column = self.column,columnspan = self.number_boxes,sticky =nsew,padx = 1,pady = 1)
self.choice_titles。 append(self.choice_title)
self.box [key] = Entry(self.frame_table,bg ='white',borderwidth = 0,width = 10,justify =center,fg ='grey')
self.box [key] .grid(row = row_number + add,column = self.column + i,sticky ='nsew',padx = 1,pady = 1)
self.box [key] .insert(0,Value%g%float(i + 1))
self.box [key] .bind(< Button-1>,lambda event,key = key,index = i :self.callback(event,key,index))
self.boxes.append(self.box [key])


def callback(self,event,key,索引):

for not self.clicked [index]:
self.box [key] .delete(0,END)
self.box [key] .config fg ='black')
self.clicked [index] = True

print self.box [key] .get()
pre>

解决方案

你至少有几个选择。例如,您可以将条目窗口小部件存储在dict或list列表中,然后将索引传递到回调中。例如:

  self.box = {} 
for i in xrange(self.number_boxes):
row = row_number + add
column = self.column + i
key =%d /%d%(行,列)
...
self.box [key] = Entry(...)
self.box [key] .bind(< Button-1>,lambda event,key = key:self.callback $ b

重要的是不只是选择一个人在互联网上给你的方法,问题和您必须解决的工具。如果您花时间了解lambda的工作原理(或 functools.partial ) ),并且对基本数据结构(如列表和定义)有基本的了解,这样的问题将不再是阻碍。


In the code below I need to return all values returned (but for each row separately) but I have tried lambda and failed and I'm not getting anywhere AGAIN.

I also have the callback definition where it previously worked and when I click in a box, it deletes the grey writing so I can continue to input in black.

If possible I would like to add the value return for the entry box at the end of my callback function rather than keeping 'numberwritten' and 'callback'..Is this possible?

The number of boxes added is equal to self.number_boxes and can be from 1 to n.

Thank you.

UPDATED CODE:

    self.numbers = [StringVar() for i in xrange(self.number_boxes) ] 

    for i in xrange(self.number_boxes):
          row = (row_number +add)
          column = self.column +i
          key = "%d/%d" % (row,column)
          self.clicked = ([False] * self.number_boxes)
          self.choice_title = Label(self.frame_table, bg=self.mycolour, borderwidth=0, width=10) 
          self.choice_title.grid(row=1, column=self.column, columnspan=self.number_boxes, sticky="nsew", padx=1, pady=1) 
          self.choice_titles.append(self.choice_title)
          self.box[key] = Entry(self.frame_table,bg='white',borderwidth=0, width=10, justify="center", fg='grey')
          self.box[key].grid(row=row_number+add,column=self.column+i, sticky='nsew', padx=1, pady=1) 
          self.box[key].insert(0, "Value %g" % float(i+1))
          self.box[key].bind("<Button-1>", lambda event, key=key, index=i : self.callback(event, key, index))
          self.boxes.append(self.box[key])


def callback(self, event, key, index):

    for not self.clicked[index]:
         self.box[key].delete(0, END)
         self.box[key].config(fg='black')
         self.clicked[index] = True

         print self.box[key].get()

解决方案

You have at least a couple of choices. For one, you can store store the entry widgets in a dict or list of lists, then pass the index into the callback. For example:

self.box = {}
for i in xrange(self.number_boxes):
    row = row_number+add
    column = self.column+i
    key = "%d/%d" % (row,column)
    ...
    self.box[key] = Entry(...)
    self.box[key].bind("<Button-1>", lambda event, key=key: self.callback(event, key))

The important thing is to not just pick a method somebody gives you on the internet, but to understand the problem and the tools you have to solve it. If you take the time to learn how lambda works (or functools.partial) and have a basic understanding of fundamental data structures like lists and dicts, problems like this will cease to be stumbling blocks.

这篇关于回调和n输入框小部件不起作用Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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