在'n'Entry小部件Tkinter界面中保存变量 [英] Saving variables in 'n' Entry widgets Tkinter interface

查看:58
本文介绍了在'n'Entry小部件Tkinter界面中保存变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对代码的长度表示歉意,但我想展示所有内容.

Firstly apologises for the length of code but I wanted to show it all.

我有一个看起来像这样的界面:

I have an interface that looks like this:

当我将第三个选项菜单更改为列表"时,我将在选项中添加具有"n"个值(未显示).然后,我需要'n'列,用户可以在其中输入其值.

When I change the third Option Menu to "List" I will add in the option to have 'n' values (not shown). I then require 'n' columns where the user can input their values.

我还有一个问题,根据接口打开的文本文件,行数可能为'n'.

I also have the problem where there may be 'n' amount of rows depending on a text file opened by the interface.

因此,我想知道是否可以(如我在框内不重复相同的值而遇到困难,现在我需要'n'列)添加"n"行和列的数量,如我的代码所示,只需添加4列.我可以读取确定的行数,但是很难根据所有行数返回所有这些值.到目前为止,我只能进行一行操作.

I am therefore wondering if it possible (as I'm having difficulties not repeating the same values in boxes and now I require 'n' columns) to add 'n' amount of rows and columns as my code shows with just adding 4 columns. I can read the amount of rows ok but have trouble returning all of these values depending on how many rows there are. So far I can do one row..

谢谢!

def numberwritten(number): 
    fg = number.get()
    print fg

numbers = [StringVar() for i in xrange(4) ] #Name available in global scope. Need to add other rows?
for i in numbers: 
    i.trace('w',lambda a,b,c,n=i: numberwritten(n) ) 


def ChoiceBox(choice):


    co_ord = str(frame_table.grid_size())
    col, rows = map(float, co_ord.strip('()').split(','))
    rows = int(rows)
    if choice == "Fixed":
        empty1.destroy()
        #choice_frame.grid_forget()     
        tkMessageBox.showinfo("Message", "No optimisation, value fixed.")
    elif choice == "List":
        column = 7
        for i in xrange(4): 
        choice_title = Label(frame_table, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=10) 
            choice_title.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1) 
            boxes=[]

        for i in xrange(4):
            for j in range(2, rows): 
                box=Entry(frame_table,bg='white',borderwidth=0,textvariable=numbers[i], width=10, justify="center") # Here I'm having problems with rows 
                box.grid(row=j,column=column+i, sticky='nsew', padx=1, pady=1) 
        boxes.append(box)
        box1,box2,box3,box4=boxes


    elif choice == "Interval" or "Optimisation":
        for i in xrange(2): 
            choice_title1 = Label(frame_table, text='Min Value', bg='white', borderwidth=0)
            choice_title1.grid(row=0, column=column, sticky="nsew", padx=1, pady=1)
            choice_title2 = Label(frame_table, text='Max Value', bg='white', borderwidth=0)
            choice_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)

            boxes=[]

        for i in xrange(2): 
            box=Entry(frame_table,bg='white',borderwidth=0,textvariable=numbers[i]) 
            box.grid(row=rows+1,column=i, sticky='ew', padx=1, pady=1) 
            boxes.append(box)
            box1,box2,box3,box4=boxes

更新:我已经略微进行了改进,ChoiceBox中现在位于类Window下的部分是:ChoiceBox(自我,选择),下面是我需要更改注释行以接受'n'金额的部分的盒子.

UPDATE: I've advanced slightly and the section in ChoiceBox which is now under a class Window: is ChoiceBox(self, choice), I have the following section where I need to change the commented line to accept 'n' amount of boxes.

column = 7
        for i in xrange(self.number_boxes): 
            choice_title = Label(self.frame_table, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=10) 
            choice_title.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1) 
        boxes=[]

        for i in xrange(self.number_boxes):
            for j in range(2, rows): 
                box=Entry(self.frame_table,bg='white',borderwidth=0,textvariable=numbers[i], width=10, justify="center") 
                box.grid(row=j,column=column+i, sticky='nsew', padx=1, pady=1) 
            boxes.append(box)
        #box1,box2,box3,box4=boxes

但是,我仍然有一个问题,就是使用数字书写方式(包括原始示例中的前几行代码)完全提取一个值列表,最好是为每一行提取值.

I still however then have the problem of using numberwritten including the first few lines of code in original example to completely extract a list of values, preferably for each row.

推荐答案

虽然我不相信您已经找到了表达接口的最简单方法,但是要做想要的事情却相对简单.

While I'm not convinced that you've found the easiest way to express your interface yet, to do what you want is relatively simple.

从概念上讲,要添加用于编辑值的条目,您必须定义(并存储对它的引用)一个对象,该对象充当该值的模型.然后,您定义将操纵该值的一个或多个小部件.然后添加必要的键绑定(通常不添加;默认值非常好).最后,将小部件添加到整个用户界面(例如,通过 grid 方法).但是,这确实意味着您必须设计整体代码来处理尺寸不固定的模型.(当然,删除条目是相反的过程.)

Conceptually, to add an entry for editing a value you have to define (and store a reference to) an object that acts as the model for the value. You then define the widget (or widgets) that will manipulate that value. Then you add such key bindings as are necessary (often none; the defaults are pretty good). Finally, you add the widget to the overall user interface (via the grid method, for example). However, this does mean that you have to design your overall code to handle a model whose size is not fixed. (Removing an entry is the reverse process, of course.)

重构代码分解为较小的代码是一个好主意,这些代码可以完成较小的任务.例如,编写一个函数来 just 创建一个模型,一个输入小部件,将该小部件添加到GUI中并返回模型对象.然后,其他需要创建此类耦合条目的地方都可以要求该专家功能为他们完成工作,因此您可以一次正确设置.通过将任务分解成更细小的部分,定义非常明确的任务,将重点放在更复杂的逻辑方面变得容易得多,而不必为小部件管理的(冗长而无聊的)细节所困扰.

It's a good idea to start refactoring your code into smaller pieces that do smaller pieces of this overall task. For example, write a function to just create a model, an entry widget, to add that widget to the GUI, and to return the model object. Everywhere else that needs to create such a coupled entry can then ask this expert function to do the job for them, so you can get it right, once. By breaking things up into smaller pieces with very clearly defined tasks, it becomes much easier to focus on the more complex logic aspects instead of getting bogged down with the (long, boring) details of widget management.

这篇关于在'n'Entry小部件Tkinter界面中保存变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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