Question

I want to create a BooleanVar() object, move_choice_S, which responds to the Button self.bttn15 and the function update_moves() and displays the content when the button is pressed in the textbox self.txt_box. I'm not sure how and where to instantiate the object move_choice_S, I've tried with move_choice_S = BooleanVar() to then use the command self.update_moves() but I keep getting AttributeError and TypeError with the errormessage that there is no instance called move_choice_S. I've also tried to import Tkinter as tk and move_choice_S = tk.BooleanVar() but I keep getting AttributeError:Application instance has no attribute 'move_choice_S'. How should I instantiate the object self.move_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()
Was it helpful?

Solution

I updated your code a bit. Now it is running without error but since you are setting your move_choice_S to True, it is writing to text only S on each button press.

EDIT: I updated move_choice_S parts. Since you defined move_choice_S in global scope, you don't need use self. to use/reach it. You can use it by move_choice_S or as @FabienAndre said, you can define it in __init__.

-option sticky needs its argument as string. So I changed those.

insert needs at least two arguments. First is index and the second is what is going to be inserted.

I imported using import xxx form, so I changed all tkinter items according to that but it's just a habit. You don't have to use this import form.

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()

OTHER TIPS

You are creating move_choice_S variable in the global namespace. It can be reached from anywhere in your program using the raw name move_choice_S.

To fix your problem, you can either:

  • access it without self in update_moves,
  • define it in the Application constructor, ie:

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

You might read this book chapter for in-depth explanation on scope in Python.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top