Python 奇怪的tk输入框行为

Python 奇怪的tk输入框行为,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我正在构建一个带有多个输入框、复选框等的简单GUI,在Tkinter.entry类中遇到了一个非常奇怪的行为,我想知道是否有其他人遇到过它和/或我只是在做一些愚蠢的事情 我创建了一个简单的类来包装每个Tkinter.Entry对象和接口。我想实现一种方法来更改每个框的宽度,所以我在类中添加了一个参数width。当我这样做时,我所有的框都被“链接”,当我在一个框中键入时,我在每个框中键入。在我实现这个功能之前,它工作得很好,当我把它拿出来时,它又能工作了。这是我的班级: import Tkinte

我正在构建一个带有多个输入框、复选框等的简单GUI,在
Tkinter.entry
类中遇到了一个非常奇怪的行为,我想知道是否有其他人遇到过它和/或我只是在做一些愚蠢的事情

我创建了一个简单的类来包装每个
Tkinter.Entry
对象和接口。我想实现一种方法来更改每个框的宽度,所以我在类中添加了一个参数
width
。当我这样做时,我所有的框都被“链接”,当我在一个框中键入时,我在每个框中键入。在我实现这个功能之前,它工作得很好,当我把它拿出来时,它又能工作了。这是我的班级:

import Tkinter as tk

class EntryBox:
    def __init__(self, master, row, column, default_val="", width=20):
        self.val = tk.StringVar()
        self.default_val = default_val
        self.width = width
        # with the width parameter specified
        self.e = tk.Entry(master, text="cb_text", textvariable=self.val, width=self.width)
        # without the width parameter specified (defaults to a width of 20)
        # self.e = tk.Entry(master, text="cb_text", textvariable=self.val)
        self.e.grid(row=row, column=column, sticky=tk.E)
        self.e.insert(0, self.default_val)

    def get_val(self):
        return self.val.get()

    def activate(self):
        self.e.config(state=tk.NORMAL)

    def deactivate(self):
        self.e.config(state=tk.DISABLED)

    def focus(self):
        self.e.focus()
这是我的程序,不包括宽度参数:

这是我的程序,包括宽度参数:

如您所见,所有默认值都会填充到每个框中,每当我编辑一个框时,我都会编辑所有框

下面是我如何实例化每个对象(我不怀疑这是个问题,但只是为了彻底):

我猜这是Tkinter中一个奇怪的bug,或者与
名称空间有关,我对它了解得不够


编辑:我更新了类代码以指定如何不包含宽度参数。在调用
tk.Entry

时,我甚至没有将其作为命名参数包含在Tkinter Entry类的继承中。我稍微操纵了你的代码

import Tkinter as tk

class EntryBox(tk.Entry):
    def __init__(self, master, row, column, default_val="", width=20):
        tk.Entry.__init__(self, master, text="cb_text")
        self.val = tk.StringVar()
        self.default_val = default_val
        self.width = width
        # with the width parameter specified
        # self.e = tk.Entry(master, text="cb_text", textvariable=self.val, width=self.width)
        # without the width parameter specified (defaults to a width of 20)
        # self.e = tk.Entry(master, text="cb_text", textvariable=self.val)
        # self.e.grid(row=row, column=column, sticky=tk.E)
        self.config(textvariable=self.val, width=self.width)
        self.insert(0, self.default_val)
        self.grid(row=row, column=column, sticky=tk.E)


    def get_val(self):
        return self.val.get()

    def activate(self):
        self.e.config(state=tk.NORMAL)

    def deactivate(self):
        self.e.config(state=tk.DISABLED)

    def focus(self):
        self.e.focus()

不幸的是,我没有足够的代表点直接发表评论。

删除属性
text='cb\u text'
。我不知道你认为这是在做什么,但是
text
只是
textvariable
的缩写,所以使用它和做
输入一样(…,textvariable='cb_text',textvariable=self.var
,…)


显然tkinter Entry小部件处理命名参数的方式有一个错误,当您添加width参数时会触发该错误。每个条目小部件的
textvariable
属性设置为
“cb_text”
,这意味着它们都共享相同的值存储。

只是用一个更模糊的名称(
box_width
)重试了一次,程序仍然表现出这种行为。只是更新了问题以澄清。即使这是一个问题(宽度默认为20),这也不能解释奇怪的“全部填充在一起”问题。而且,这可能是一个评论,而不是一个答案。哈哈,当我开始使用Tkinter时,我想我可以像你一样用复选框标记条目小部件。这就是遗留下来的旧代码。删除它修复了它!非常感谢你。教我不要离开那些似乎什么都没做的旧代码!
import Tkinter as tk

class EntryBox(tk.Entry):
    def __init__(self, master, row, column, default_val="", width=20):
        tk.Entry.__init__(self, master, text="cb_text")
        self.val = tk.StringVar()
        self.default_val = default_val
        self.width = width
        # with the width parameter specified
        # self.e = tk.Entry(master, text="cb_text", textvariable=self.val, width=self.width)
        # without the width parameter specified (defaults to a width of 20)
        # self.e = tk.Entry(master, text="cb_text", textvariable=self.val)
        # self.e.grid(row=row, column=column, sticky=tk.E)
        self.config(textvariable=self.val, width=self.width)
        self.insert(0, self.default_val)
        self.grid(row=row, column=column, sticky=tk.E)


    def get_val(self):
        return self.val.get()

    def activate(self):
        self.e.config(state=tk.NORMAL)

    def deactivate(self):
        self.e.config(state=tk.DISABLED)

    def focus(self):
        self.e.focus()