Re: [Tutor] Tkinter entry box text changed event
Phil wrote: > Again, thank you for reading this. > > I would like a function to be called when I enter text and then tab to the > next entry box. I've been trying to follow the answers to similar > questions in Stack Overflow but I've become hopelessly confused because of > the different answers given to seemingly the same question. > > I have created a row of entry boxes and a matching function like this: > > for i in range(8): > self.numbers[i]= Entry(master, width=4, justify=CENTER, > foreground="gray") self.numbers[i].grid(row=16, column=i) > self.numbers[i].bind('StringVar()', self.my_function) > > def my_function(event): > print("function called") To be consistent with the other code snippet this should be a method, not a function. > > The function is not called and I know that the binding of the function to > the entry boxes is not the correct method. What am I missing? > What the ... did you expect from the "StringVar()" argument? You can find valid events here: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-types.html Here's a simple example without a class: import Tkinter as tk def welcome(event): print("Welcome") def bye(event): print("Bye!") root = tk.Tk() for i in range(3): entry = tk.Entry(root) entry.bind("", welcome) entry.bind("", bye) entry.pack() root.mainloop() If you want to pass the index of the entry, say, you can use the extra-arguments trick, http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/extra-args.html also shown in one of my previous answers. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter entry box text changed event
On 10/04/17 05:43, Phil wrote: > I would like a function to be called when I enter text > and then tab to the next entry box. One of the things about Entry boxes is that they are extremely flexible and have many event types associated with them. The consequence of this is that you as a programmer need to be very, very specific in deciding which events you want to bind to: Enter text includes events such as key-press, key-release. These catch individual keystrokes. Then there are the navigation bindings such as focusIn and FocusOut for entering and leaving the Entry(regardless of whether you change anything. And of course you have mouse events to consider too. And you also have the StringVar() mechanism which auto detects changed values and assigns them to the nominated textvariable. So do you want to trigger your code when keys are pressed? or when the user leaves the box? or when the user arrives in the next box? And do you only want to do this when the user is tabbing around in sequence? Or what if they randomly select one of the entry boxes using the mouse? What if they use the mouse to select boxes rather than the tab key? Once you know that you will know which event types you want to bind to. But 'StringVar()' is not one of them... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] counting function calls
Dear experts, I have written the following code for motion detection with a PIR sensor with a function and I need to count how many times the funtion is called, but I get a traceback: #!/usr/bin/python3 import sys, time import RPi.GPIO as gpio gpio.setmode(gpio.BOARD) gpio.setup(23, gpio.IN) count = 0 def mein_callback(pin): count += 1 print('PIR 1 aktiviert', count) return try: gpio.add_event_detect(23, gpio.RISING, callback = mein_callback) while True: time.sleep(2) except KeyboardInterrupt: print('PIR deaktiviert') PIR 1 aktiviert Traceback (most recent call last): File "./PIRex.py", line 9, in mein_callback count += 1 UnboundLocalError: local variable 'count' referenced before assignment ^CPIR deaktiviert Tanks for help, marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft. https://www.avast.com/antivirus ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting function calls
On 10/04/17 08:55, marcus lütolf wrote: > Dear experts, > I have written the following code for motion detection with a PIR sensor > with a function and > I need to count how many times the funtion is called, but I get a traceback: > > #!/usr/bin/python3 > import sys, time > import RPi.GPIO as gpio > > gpio.setmode(gpio.BOARD) > gpio.setup(23, gpio.IN) > count = 0 > def mein_callback(pin): > count += 1 > print('PIR 1 aktiviert', count) > return > UnboundLocalError: local variable 'count' referenced before assignment > ^CPIR deaktiviert You are trying to modify a variable defined in the module or global scope. To do that you must tell Python that it is the global variable you mean. You do that by adding global count at the top of your function: def mein_callback(pin): global count # use the global variable count += 1 print('PIR 1 aktiviert', count) return You don;t need the return since Python returns None automatically at the end of a function. But it doesn't do any harm either... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter entry box text changed event
On Mon, 10 Apr 2017 09:31:10 +0200 Peter Otten <__pete...@web.de> wrote: > entry.bind("", bye) Thank you Peter and Alan, I had tried key-press but that caused the error message shown bellow which made me think that I was not on the correct track. So in desperation, after hours of frustration, I tried StringVar() because I'd seen that in a Stack overflow answer. Adapting Peter's example I have: self.numbers[i].bind("", self.my_method) def my_method(self.event): print("method called") (self.event) is a syntax error and if I leave off "self", this is the result: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__ return self.func(*args) TypeError: my_method() takes 1 positional argument but 2 were given I must be close, surely. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter entry box text changed event
On 10/04/17 10:18, Phil wrote: > def my_method(self.event): > print("method called") > > (self.event) is a syntax error and if I leave off "self", this is the result: You want two parameters self becaiuse its a method of a class so must have a self event which is the event passsed by the GUI So: def my_method(self, event): print("method called with ",event) > I must be close, surely. A comma instead of a dot... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter entry box text changed event
On Mon, 10 Apr 2017 19:40:01 +0100 Alan Gauld via Tutor wrote: > You want two parameters > self becaiuse its a method of a class so must have a self > event which is the event passsed by the GUI > So: > > def my_method(self, event): > print("method called with ",event) > > > > I must be close, surely. > > A comma instead of a dot... > Thank you so much for your patience Alan. I woke during the early hours thinking about the requirement for two parameters and realised that my other methods only have self as a single parameter and wondered if "event" was the other parameter. I hadn't though of printing the event. Take no notice of this message's posting time, I started my Raspberry Pi before the modem had established an Internet connection. It's pre sunrise here. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] [PYTHON27] How to save into .npy file?
Hi. Is there a way to save module type data into .npy file that can be used latter? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting function calls
On 04/10/2017 01:55 AM, marcus lütolf wrote: > Dear experts, > I have written the following code for motion detection with a PIR sensor > with a function and > I need to count how many times the funtion is called, but I get a traceback: > > #!/usr/bin/python3 > import sys, time > import RPi.GPIO as gpio > > gpio.setmode(gpio.BOARD) > gpio.setup(23, gpio.IN) > count = 0 > def mein_callback(pin): > count += 1 > print('PIR 1 aktiviert', count) > return > > try: > gpio.add_event_detect(23, gpio.RISING, callback = mein_callback) > while True: > time.sleep(2) > except KeyboardInterrupt: > print('PIR deaktiviert') > > PIR 1 aktiviert > Traceback (most recent call last): > File "./PIRex.py", line 9, in mein_callback > count += 1 > UnboundLocalError: local variable 'count' referenced before assignment > ^CPIR deaktiviert > > Tanks for help, marcus. Yes, what Python does here may be surprising at first: if you only read-access a global variable in a local (function in this case) scope, it gives you the value just fine. If you however try to save something to a global variable what happens is it creates a local variable, *unless* you have previously informed Python you mean the global one, by using the global statement as Alan listed. The specific error you see is because in order to increment 'count' (which Python has already figured out has to be local because it will be assigned to) you have to read the existing value first, but there is no existing value in the local scope. The Python programming FAQ has a short explanation of why this might be so: https://docs.python.org/2/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [PYTHON27] How to save into .npy file?
On Mon, Apr 10, 2017 at 02:10:34PM +, Allan Tanaka via Tutor wrote: > Hi. > Is there a way to save module type data into .npy file that can be used > latter? What's "module type data"? What's a .npy file? To answer your question, literally, the answer is "Yes, of course. Python can save ANY data into a file with ANY file extention". But I guess that answer isn't helpful to you if you need to save specific data to a specific file format. But without knowing what that specific data is, and the specific format, how can we answer? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What would be good use cases for the enum module?
On Sun, Apr 09, 2017 at 10:21:49AM -0500, boB Stepp wrote: > A general question about PEPs: Is there generally a PEP preceding the > addition of any new feature to the core language or the standard > library? I gather that even an accepted PEP (In this instance PEP > 0435.) may not reflect the final form of the added feature(s) as I did > not see direct mention of "auto", though that feature was alluded to. It depends on the natural of the new feature. Generally only large, complex or controversial changes require a PEP. Simple bug fixes or small additions to existing functionality don't. Once a feature/module has been added to the language, the PEP usually stops being updated. So it effectively becomes a snapshot of what the feature was intended to look like in Version X when it was first added, not necessarily what it looks like in Version X+1. > > Enums have a few other nice properties, which you may or may not care > > about, but the primary use is to act as set of related named symbols. > > > > The iterability looks useful. IntEnum looks interesting. IntFlag and > Flag I am currently clueless about. I would have to dig deeper as I > don't fully understand their uses yet. Basically IntEnum and similar exist for compatability with C-style enums, which are basically integers. That lets you combine flags with bitwise OR. Sometimes that's useful, but its not the default for Python enums. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor