Python 在Tkinter应用程序中创建BooleanVar()对象时出现TypeError和AttributeError

Python 在Tkinter应用程序中创建BooleanVar()对象时出现TypeError和AttributeError,python,tkinter,typeerror,tk,attributeerror,Python,Tkinter,Typeerror,Tk,Attributeerror,我想创建一个BooleanVar()对象move_choice_S,它响应按钮self.bttn15 函数update_moves()并在textbox self.txt_框中按下按钮时显示内容。我不确定如何以及在何处实例化对象move_choice_S,我尝试使用move_choice_S=BooleanVar()命令self.update_moves(),但我不断得到AttributeError和TypeError,错误消息是没有名为move_choice_S的实例。我还尝试将Tkinter

我想创建一个BooleanVar()对象move_choice_S,它响应按钮self.bttn15 函数update_moves()并在textbox self.txt_框中按下按钮时显示内容。我不确定如何以及在何处实例化对象move_choice_S,我尝试使用move_choice_S=BooleanVar()命令self.update_moves(),但我不断得到AttributeError和TypeError,错误消息是没有名为move_choice_S的实例。我还尝试将Tkinter作为tk和move_choice_S=tk.BooleanVar()导入但我一直得到AttributeError:应用程序实例没有属性“move\u choice\S”。我应该如何实例化对象self.move\u choice\S

class Application(Frame):


    def __init__(self, master):
        """ init the frame """
        Frame.__init__(self) 
        self.grid()
        self.create_widgets()

    def create_widgets(self):

       self.bttn15 = Button(self, text = "OK", variable = self.move_choice_S, command =  self.update_moves)
        self.bttn15.grid(row = 14, column = 4, sticky = W)

      # message
        self.box_txt = Text(self, width = 65, height = 25, wrap = WORD)
        self.box_txt.grid(row = 15, column = 0, columnspan = 5, sticky = W)


   def update_moves(self):

        moves = ""
        if self.move_choice_S.get():
            moves = "S"

        self.box_txt.insert(0.0, END) 


        self.box_txt.insert(0.0, moves) 


# Main
root = tk.Tk()

move_choice_S = tk.BooleanVar()
move_choice_S.set(True)
move_choice_S.get()


root.title("Maltparser1.0_demo")

root.geometry("900x700")

app = Application(root)
root.mainloop()

我更新了你的代码。现在它正在运行,没有错误,但由于您正在将
move\u choice\u S
设置为
True
,因此每次按下按钮时,它只写入
text

编辑:我更新了
移动选择
部分。因为您在全局范围内定义了
move\u choice\S
,所以不需要使用
self.
来使用/到达它。您可以通过
move\u choice\u S
使用它,或者正如@FabienAndre所说的,您可以在
\uuuu init\uuu
中定义它

-选项sticky
需要其参数作为字符串。所以我改了

insert
至少需要两个参数。第一个是索引,第二个是要插入的内容

我使用
import xxx
表单导入,因此我根据该表单更改了所有
tkinter
项,但这只是一种习惯。您不必使用此
导入
表单

import Tkinter as tk

class Application(tk.Frame):
    def __init__(self, master):
        """ init the frame """
        tk.Frame.__init__(self) 
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.bttn15 = tk.Button(self, text = "OK", command = self.update_moves)
        self.bttn15.grid(row = 14, column = 4, sticky = "W")

        # message
        self.box_txt = tk.Text(self, width = 65, height = 25, wrap = "word")
        self.box_txt.grid(row = 15, column = 0, columnspan = 5, sticky = "W")

    def update_moves(self):

        moves = ""
        if move_choice_S.get():
            moves = "S"

        self.box_txt.insert(0.0, moves + "\n") 

# Main
root = tk.Tk()

move_choice_S = tk.BooleanVar()
move_choice_S.set(True)
move_choice_S.get()


root.title("Maltparser1.0_demo")

root.geometry("900x700")

app = Application(root)
root.mainloop()

您正在全局命名空间中创建
move\u choice\S
变量。可以使用原始名称
move\u choice\u S
从程序中的任何位置访问它

要解决问题,您可以:

  • update\u moves
    中不使用
    self
    访问它
  • 应用程序中定义它,即:

    class Application(Frame):
        def __init__(self, master):
            (...)
            self.move_choice_S = BooleanVar()
    

您可以阅读Python中有关作用域的深入解释。

在这段代码中,您得到的是“AttributeError:Application instance没有属性'move_choice_S'”,因为move_choice_是在main中创建的,而不是在类应用程序中创建的,所以您不需要“self”来“处理”这段代码。由于您在运行代码时就将变量设置为True,因此在您按下按钮后,它将始终插入S。那么您的tkinter、python版本是什么?我使用的是python 2.6.6,关于选项变量,我不断收到相同的错误。如果您描述您所更改的内容,您的答案会更好。现在,读者被迫逐行比较,看看你做了什么。