Then we’ll expand the buttons out and make it look a little nicer. This creates three individual buttons and then gives them text inside, as well as indicating what def they need to pull in. A = tkinter.Button(text = "preview", command = preview_cam)ī = tkinter.Button(text = "video", command = cap_vid)Ĭ = tkinter.Button(text = "photo", command = capture) This creates the box and labels it on the top bar. TKinter is a bit weird, it’s why I asked you to import both the whole thing and a specific section, as when I was building this script, this was what got it working. This starts a preview and then time.sleep(10) only has it last for ten seconds. We now need a dedicated preview button: def preview_cam(): cam.start_preview() Once done, the print(“saved”) tells users that it’s done. Obviously, we need to indicate that we’re recording, so print(“recording…”) handles this for us and the two bits sandwiching it, cam.start_preview and cam.stop_preview allow us to see what we’re capturing. Same as before for global output, except now we need to call in the recording functions, so cam.start_recording will bring in that and the brackets with output tells it to use the file name we’ve set. Next, let’s make one for capturing video: def cap_vid(): global output Here, we’re telling Python that Capture is going to look at the global output that we set to blank, then replace that blank with the time and date.Ĭam.capture(output) tells it to then name the file that we set as output.įinally, print is Python’s own version of repeating back what you put in, so we’ll tell it to say saved to tell the user that things are a-okay. Output = strftime("/home/pi/Desktop/image-%d-%m %H:%M-%S.jpg", gmtime()) This way we can do a bunch of messy code things at the top of the page and then simplify it as we go down.įirst, let’s define the photo capturing of our code: def capture(): global output ‘def’ in Python allows us to define what we want a specific piece of code to do by condensing it into a word, string, or letter. This will allow us to work with pop-up boxes. Import Tkinter with both import tkinter and from tkinter import messagebox. Tkinter is the part of Python that generates a user interface for those of us without hardware. If you’re getting errors when working on things and it revolves around no module for Tkinter, open a terminal and type in: Then the last one is Tkinter, which we’ll have to install separately in some cases. While we’re importing time, we need to also ask it to import from time to focus on gmtime and strftime. Of course, we need to work with the Pi Camera itself, but we need the commands. What we’re asking Python to bring in is to look at the GPIO pins via gpiozero and then asking it to only really focus on Button functions. We need to tell Python what we want to pull in to work with, otherwise, it’ll not understand what we need to do. Open Thonny ( Raspberry > Programming > Thonny) and then start a new file by saving it (CTRL+S) and name it whatever you want. Python is a complex beast, but it’s pretty easy to see what it’s trying to do when you breathe and look closer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |