Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编辑网格中特殊位置上的条目_Python_Dictionary_Grid_Tkinter - Fatal编程技术网

Python 编辑网格中特殊位置上的条目

Python 编辑网格中特殊位置上的条目,python,dictionary,grid,tkinter,Python,Dictionary,Grid,Tkinter,我有一个通过grid()创建条目的函数,如下所示:(将while循环替换为function,稍后应该会显示它的样子) 从Tkinter导入* root=Tk() 索引=0 当指数

我有一个通过grid()创建条目的函数,如下所示:(将while循环替换为function,稍后应该会显示它的样子)

从Tkinter导入*
root=Tk()
索引=0
当指数<10时:
col0=文本(根,宽度=20,高度=1,bg='white')
col0.grid(行=索引,列=0)
col0.insert('0.0',“名称”)
col1=文本(根,宽度=20,高度=1,bg='white')
col1.grid(行=索引,列=1)
col1.插入('0.0',“属性#1”)
col2=文本(根,宽度=20,高度=1,bg='white')
col2.grid(行=索引,列=2)
col2.insert('0.0',“属性#2”)
col3=文本(根,宽度=20,高度=1,bg='white')
col3.grid(行=索引,列=3)
col3.插入('0.0',“计数器”)
指数+=1
root.mainloop()
因此,在特殊事件中,调用函数并创建一个新条目(新行)。但是如果已经有这个事件的条目,我只想增加计数器

EventName及其entryrows(索引)保存在一个字典中,因此我有行和列,但是现在我如何实际访问col3呢

我计划通过col3.get()获取计数器,但它在每一行中都被称为col3,因此如何指定它

是否可以将col0、col1、col2等放入某种结构(如字典),以便通过col0[name].insert()访问它们。。。(名称是唯一的)?我试过了,但没有成功(我希望这并不意味着不可能,我只是对python很陌生)


有人对我的问题有什么建议或解决办法吗?

你的词典想法很好。我在这里提出了一个简单的实现。。。(类是在函数调用之间保持数据持久性的最佳方法。)

在本例中,可以通过
app.ObjGrid[(行,列)]
访问文本小部件,或者通过
self.ObjGrid[(行,列)]
访问文本小部件(如果您是通过一种方法执行)

import Tkinter as tk

class TextGrid(tk.Frame):
    def __init__(self,master):
        tk.Frame.__init__(self,master)
        self.ObjGrid={}
        self.index=1
        for i in xrange(10):
            self.addRow()

    def addRow(self,index=None):
        """
        add a row of widgets at row=<index>.  If index is omitted, add the next row
        If index is on the grid already, prints "Increment counter?" since I don't
        know what you mean by that
        """

        if(index is None):
           index=self.index+1
           self.index+=1
        if (index,0) in self.ObjGrid:
            print "Increment counter?"  #probably replace this code...
            return

        self.ObjGrid[(index,0)] = col0 = tk.Text(self,width=20, height=1, bg='white')
        col0.grid(row=index,column=0)
        col0.insert('0.0',"name")
        self.ObjGrid[(index,1)] = col1 = tk.Text(self,width=20, height=1, bg='white')
        col1.grid(row=index,column=1)
        col1.insert('0.0',"attribute #1")
        self.ObjGrid[(index,2)] = col2 = tk.Text(self,width=20, height=1, bg='white')
        col2.grid(row=index,column=2)
        col2.insert('0.0',"attribute #2")
        self.ObjGrid[(index,3)] = col3 = tk.Text(self,width=20, height=1, bg='white')
        col3.grid(row=index,column=3)
        col3.insert('0.0',"counter")

if __name__ == "__main__":
    root=tk.Tk()
    app=TextGrid(root)
    app.grid(row=0,column=0)
    app.addRow(3) #Shouldn't do anything
    app.addRow() #Should add another row to the end.
    print(type(app.ObjGrid[(2,3)])) #tkinter text widget.
    root.mainloop()
人们通常会皱眉

import XXX as short_name_that_is_easy_to_remember

一般来说,这是首选。此外,在Tkinter做事情时,我发现从一节课开始总是最容易的。构建一个小部件。将小部件更改为应用程序很简单,但将应用程序更改为小部件几乎是不可能的。构建小部件最简单的方法是继承
框架
,然后将其他小部件打包到该框架上,如我上面所示。

您的字典想法很好。我在这里提出了一个简单的实现。。。(类是在函数调用之间保持数据持久性的最佳方法。)

在本例中,可以通过
app.ObjGrid[(行,列)]
访问文本小部件,或者通过
self.ObjGrid[(行,列)]
访问文本小部件(如果您是通过一种方法执行)

import Tkinter as tk

class TextGrid(tk.Frame):
    def __init__(self,master):
        tk.Frame.__init__(self,master)
        self.ObjGrid={}
        self.index=1
        for i in xrange(10):
            self.addRow()

    def addRow(self,index=None):
        """
        add a row of widgets at row=<index>.  If index is omitted, add the next row
        If index is on the grid already, prints "Increment counter?" since I don't
        know what you mean by that
        """

        if(index is None):
           index=self.index+1
           self.index+=1
        if (index,0) in self.ObjGrid:
            print "Increment counter?"  #probably replace this code...
            return

        self.ObjGrid[(index,0)] = col0 = tk.Text(self,width=20, height=1, bg='white')
        col0.grid(row=index,column=0)
        col0.insert('0.0',"name")
        self.ObjGrid[(index,1)] = col1 = tk.Text(self,width=20, height=1, bg='white')
        col1.grid(row=index,column=1)
        col1.insert('0.0',"attribute #1")
        self.ObjGrid[(index,2)] = col2 = tk.Text(self,width=20, height=1, bg='white')
        col2.grid(row=index,column=2)
        col2.insert('0.0',"attribute #2")
        self.ObjGrid[(index,3)] = col3 = tk.Text(self,width=20, height=1, bg='white')
        col3.grid(row=index,column=3)
        col3.insert('0.0',"counter")

if __name__ == "__main__":
    root=tk.Tk()
    app=TextGrid(root)
    app.grid(row=0,column=0)
    app.addRow(3) #Shouldn't do anything
    app.addRow() #Should add another row to the end.
    print(type(app.ObjGrid[(2,3)])) #tkinter text widget.
    root.mainloop()
人们通常会皱眉

import XXX as short_name_that_is_easy_to_remember

一般来说,这是首选。此外,在Tkinter做事情时,我发现从一节课开始总是最容易的。构建一个小部件。将小部件更改为应用程序很简单,但将应用程序更改为小部件几乎是不可能的。构建小部件最简单的方法是继承
框架
,然后将其他小部件打包到该框架上,如上所示。

您需要保存对所创建的所有文本小部件的引用:

colHeaders = ["name", "attribute #1", "attribute #2", "counter"]
myTextWidgets = []
for index in range(10):
    subList = []
    for i, header in enumerate(colHeaders):
        text = Text(root,width=20, height=1, bg='white')
        text.grid(row=index,column=i)
        text.insert('0.0',header)
        subList.append(text)
    myTextWidgets.append(subList)
这将允许您通过其行/列访问每个小部件:

myTextWidgets[0][1].config(bg="purple")
您可以很容易地将新行附加到此结构,如下所示:

subList = []
for i, header in enumerate(colHeaders):
    text = Text(root,width=20, height=1, bg='white')
    text.grid(row=len(myTextWidgets),column=i)
    text.insert('0.0', header)
    subList.append(text)
myTextWidgets.append(subList)

使用此结构,唯一的不是名称,而是每个单元格的位置。

您需要保存对所创建的所有文本小部件的引用:

colHeaders = ["name", "attribute #1", "attribute #2", "counter"]
myTextWidgets = []
for index in range(10):
    subList = []
    for i, header in enumerate(colHeaders):
        text = Text(root,width=20, height=1, bg='white')
        text.grid(row=index,column=i)
        text.insert('0.0',header)
        subList.append(text)
    myTextWidgets.append(subList)
这将允许您通过其行/列访问每个小部件:

myTextWidgets[0][1].config(bg="purple")
您可以很容易地将新行附加到此结构,如下所示:

subList = []
for i, header in enumerate(colHeaders):
    text = Text(root,width=20, height=1, bg='white')
    text.grid(row=len(myTextWidgets),column=i)
    text.insert('0.0', header)
    subList.append(text)
myTextWidgets.append(subList)

在这种结构中,不是名称是唯一的,而是每个单元格的位置。

不仅可能,我认为在这种情况下更可取。这里有一个简单的方法:

attr_names = ['name', 'attribute #1', 'attribute #2', 'counter']
def make_text_field(row, col, attr_name):
    text = Text(root, width=20, height=1, bg='white')
    text.grid(row=row, column=col)
    text.insert('0.0', attr_name)

text_fields = {}
for row, col in itertools.product(xrange(10), xrange(4)):
    text_fields[row, col] = make_text_field(row, col, attr_names[col])
list_of_cols = list()
for col in xrange(4):
    col = list()
    list_of_cols.append(col)
    for row in xrange(10):
        col.append(make_text_field(row, col, attr_names[col]))
这将每个字段分别存储在
文本\u字段中
,其中
(行,列)
元组作为键。您还可以创建一个列表列表;因为您希望访问整个列,所以您可能更喜欢这样。您可以预先分配列表并如上所述使用
产品
,但为简单起见,这里有另一种方法:

attr_names = ['name', 'attribute #1', 'attribute #2', 'counter']
def make_text_field(row, col, attr_name):
    text = Text(root, width=20, height=1, bg='white')
    text.grid(row=row, column=col)
    text.insert('0.0', attr_name)

text_fields = {}
for row, col in itertools.product(xrange(10), xrange(4)):
    text_fields[row, col] = make_text_field(row, col, attr_names[col])
list_of_cols = list()
for col in xrange(4):
    col = list()
    list_of_cols.append(col)
    for row in xrange(10):
        col.append(make_text_field(row, col, attr_names[col]))

这不仅是可能的,我想说在这样的情况下更可取。这里有一个简单的方法:

attr_names = ['name', 'attribute #1', 'attribute #2', 'counter']
def make_text_field(row, col, attr_name):
    text = Text(root, width=20, height=1, bg='white')
    text.grid(row=row, column=col)
    text.insert('0.0', attr_name)

text_fields = {}
for row, col in itertools.product(xrange(10), xrange(4)):
    text_fields[row, col] = make_text_field(row, col, attr_names[col])
list_of_cols = list()
for col in xrange(4):
    col = list()
    list_of_cols.append(col)
    for row in xrange(10):
        col.append(make_text_field(row, col, attr_names[col]))
这将每个字段分别存储在
文本\u字段中
,其中
(行,列)
元组作为键。您还可以创建一个列表列表;因为您希望访问整个列,所以您可能更喜欢这样。您可以预先分配列表并如上所述使用
产品
,但为简单起见,这里有另一种方法:

attr_names = ['name', 'attribute #1', 'attribute #2', 'counter']
def make_text_field(row, col, attr_name):
    text = Text(root, width=20, height=1, bg='white')
    text.grid(row=row, column=col)
    text.insert('0.0', attr_name)

text_fields = {}
for row, col in itertools.product(xrange(10), xrange(4)):
    text_fields[row, col] = make_text_field(row, col, attr_names[col])
list_of_cols = list()
for col in xrange(4):
    col = list()
    list_of_cols.append(col)
    for row in xrange(10):
        col.append(make_text_field(row, col, attr_names[col]))

对范围(10)中的索引使用
而不是
,而对范围(10)中的索引使用
而不是
而不是
而索引此解决方案效果很好,对其进行了一些调整以适合我的程序,但列表中的列表正是我的目标。谢谢!这个解决方案运行得很好,对它进行了一些调整以适合我的程序,但列表中的列表正是我的目标。谢谢!我已经在代码中完成了导入工作,类也是如此;)上面的代码只是为了解释我的问题,但还是要感谢您的回答。我已经在代码中完成了导入工作,对类也是如此;)上面的代码只是为了解释我的问题,但还是谢谢你的回答。