-1

I'm coding a simple gui with tkinter and python. Here's the code:

from tkinter import *

class migrate_tk(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.geometry("400x400")
        self.parent = parent        
        self.initialize()

    def initialize(self):           
        self.grid()                 

        self.entry = Entry(self)                            
        self.entry.grid(column=0, row=0, sticky='EW')       
        #~ self.entry.focus_set()
        self.entry.bind("<Return>", self.OnPressEnter)        
        #~ self.bind('<Return>', self.OnPressEnter)

        label = Label(self, anchor="w", fg="white", bg="blue")
        label.grid(column=0, row=1, columnspan=2, sticky="EW")

        self.grid_columnconfigure(0, weight=1)          

        self.resizable(True, False)                     

    def OnPressEnter(self,event):
        print ("You pressed enter")
        if self.label["bg"] == "blue":
            self.label["bg"] = "yellow"
        else: self.label["bg"] = "blue"


if __name__ == "__main__":
    app = migrate_tk(None)
    app.title('app') 
    app.mainloop()

The problem is that pressing the return key, it doesn't print anything (obviously i run it into a terminal) Thanks!

bomba
  • 153
  • 1
  • 2
  • 13
  • 1
    If you could reduce your example down to just the necessary code to explain the question, that would help. – Andrew May 29 '14 at 10:23
  • When I run your code I get a stack trace, but I _also_ get "You pressed enter") and the label changes to yellow. So it seems that the binding is working fine. You're forgetting to assign `self.label`, however, which is the reason you're getting a stack trace. – Bryan Oakley May 29 '14 at 10:58

1 Answers1

0

The problem you might be getting is:

AttributeError: 'tkapp' object has no attribute 'label'

& the reason for that is: you have to do this while declaring label:

self.label = Label(self, anchor="w", fg="white", bg="blue")
self.label.grid(column=0, row=1, columnspan=2, sticky="EW")

Down below you were using label as an instance of class migrate_tk but not when you declared it

The enter widget doesn't get focus automatically,so when you press Enter without selecting the widget,it doesn't do anything.

What you need to do is un-comment the code:

self.entry.focus_set()

& It will work fine after that.

Alok
  • 2,629
  • 4
  • 28
  • 40
  • I understand the label error, but not why the print command of OnPressEnter doesn't work. Anyways, I don't get the attribute error. – bomba May 29 '14 at 10:30
  • well that's the error i was getting when running your program(& it is an error!). Also,its working exactly like its supposed to be when i removed this error .Kindly share what error you got – Alok May 29 '14 at 10:33
  • I'm using xubuntu 14.04 and python3 (same on python2.7). It doesn't print any error. The only thing it prints is "You clicked the button!" could be a system-key binding error or some similar? but it's strange, it's occurring just here! – bomba May 29 '14 at 10:40
  • When you click the button it **is** gonna say _You clicked the button!_ & do nothing else, as there is nothing else there in _OnButtonClick()_ – Alok May 29 '14 at 10:43
  • sorry I meant the function OnPressEnter doesn't print anything. i just edited the source removing OnButtonClick to be clear – bomba May 29 '14 at 10:54
  • I'm sorry but after adding `self` to the label,it **is** printing _You pressed enter_ & also the color is changing to Yellow. – Alok May 29 '14 at 10:57
  • Okay, maybe the problem is you are not entering any value & just pressing enter(though that's not the problem). So,it is not getting the focus – Alok May 29 '14 at 10:57
  • ok, I've been hasty. Solved :) thanks for your patience – bomba May 29 '14 at 12:08
  • @bomba : if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Alok May 29 '14 at 12:09