jwjwjw1990
Posts: 9
Joined: Tue Feb 26, 2019 2:37 pm

Python GUI application reset-button and run at startup

Tue Feb 26, 2019 2:49 pm

For an escape room type game I have created a python application that runs a puzzle game on a screen. Once the game is solved, it outputs a signal to a parent system that triggers something else. I would like to enable 2 things:

1. I would like to use one of the inputs to be a reset button of the puzzle (ie. restart the application). I'm new to programming and I was given some example code. It contains Application, Controller and View from which i've derived my own puzzle-specific classes. Does this sound like a MVC-structure?

2. I also would like the application to start once the raspberry is started. I tried crontab but couldn't get it to work.

Any help here would be greatly appreciated

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 4:45 pm

Answering question #2 first, as @B.Goode pointed out in this topic: https://www.raspberrypi.org/forums/view ... 2&t=234038,
you need to be running the full version of Raspbian to for a GUI to work. I am assuming you already are.

If you look for my post within that topic I provide a simple working Tkinter example with autostart.

Now on to the first question - MVC is an architectural pattern that could be applied to your application. You could code without the MVC model, that really is up to you.

jwjwjw1990
Posts: 9
Joined: Tue Feb 26, 2019 2:37 pm

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 7:28 pm

I've tried the startup-part using the autostart method, and it only shows a black screen. I can get the terminal up by right clicking, and I tried to remove the line I added to autostart, but this doesn't change anything. How can I get back to the Raspbian Pixel desktop and not start the xlterminal?

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 8:02 pm

If you run the xx.py from a terminal, do you see a small Tkinter window? That should be what you should see plus the lxterminal as a result of the autostart. It is a very small GUI window.

When you edited out the lxterminal line from startup, are you sure you saved the file?

jwjwjw1990
Posts: 9
Joined: Tue Feb 26, 2019 2:37 pm

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 8:56 pm

No it's not showing anything. Also, I did save the file, and just checked. It's empty.

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 8:58 pm

Are you running the full version of Raspbian? If yes, can you create a simple tk GUI and run it successfully?

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 9:10 pm

To make sure you have tkinter installed, open a command window, type python3
and then enter import tkinter in the repl.

Code: Select all

pi@RPi3BP:~ $ python3
Python 3.4.9 (default, Feb 25 2019, 08:31:38) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 
If you get an ImportError and not just the >>> prompt, then you do not have tkinter installed.

jwjwjw1990
Posts: 9
Joined: Tue Feb 26, 2019 2:37 pm

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 9:14 pm

I think I am... And I can't get it to work on the Terminal Emulator in the current state. It now starts in OpenBox everytime, not on the default desktop, which I believe to be Raspbian Pixel or something like that.

Update: with 'import tkinter' I get an ImportError: No module named '_tkinter'

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 9:17 pm

Looks like the image you have does not contain tkinter. The image that I know works is " Raspbian Stretch with desktop and recommended software"
https://www.raspberrypi.org/downloads/raspbian/

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 9:45 pm

If you are not using tkinter for your application, all you need do to start your program from startup is change xx.py to whatever you are calling your program.

Code: Select all

@lxterminal -e python3 /home/pi/xx.py
If you are using python2, change python3 to python.

jwjwjw1990
Posts: 9
Joined: Tue Feb 26, 2019 2:37 pm

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 10:03 pm

I just want to get the normal GUI back. Now I can't even use right mouse click in that Open Box thing, or any keyboard shortcuts. Will the project files in home/pi be kept when I install a new OS?

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Tue Feb 26, 2019 10:54 pm

Adding or removing that line should not affect how the GUI works. If you install a new OS, you will wipe out everything and you will need to reload your files.

jwjwjw1990
Posts: 9
Joined: Tue Feb 26, 2019 2:37 pm

Re: Python GUI application reset-button and run at startup

Wed Feb 27, 2019 10:35 am

Then I probably misunderstood the meaning of GUI. Anyway, after I added the line to the autostart file, I rebooted the system and ever since, It starts with a full black screen. Right clicks showed some options like 'Exit' (which didn't work) and 'Virtual Terminal'. Some keyboard shortcut gave me settings for OpenBox where I could set the number of desktops and stuff about primairy screens. I changed some of these settings because I was hoping my application was opened in a different screen. After rebooting again, I can't even get the right click menu back, I can litterally do nothing. That's the situation I'm currently in, and I don't know how to get out of it.

User avatar
MrYsLab
Posts: 434
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Python GUI application reset-button and run at startup

Wed Feb 27, 2019 3:29 pm

There may be easier ways to do this, but I here is what I would do.

On a spare SD card install the full Raspbian OS.
Boot your Pi with the new OS.
Place your old SD card (the one with the problems) into an SD reader and then plug the reader into your Raspberry pi.
You will probably see an "Error Operation was cancelled" dialog box pop up. Just click OK to dismiss this window.

Open a terminal window and type

Code: Select all

df -Th
You should then see something similar to:

Code: Select all

pi@RPi3BP:~ $ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda2      ext4       15G  3.9G   11G  28% /
devtmpfs       devtmpfs  434M     0  434M   0% /dev
tmpfs          tmpfs     438M     0  438M   0% /dev/shm
tmpfs          tmpfs     438M   17M  421M   4% /run
tmpfs          tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs          tmpfs     438M     0  438M   0% /sys/fs/cgroup
/dev/sda1      vfat       44M   23M   22M  52% /boot
tmpfs          tmpfs      88M     0   88M   0% /run/user/1000

/dev/sda1 is the boot partition of your old sd card and /dev/sda2 is what we are interested in.

You will need to mount sda2. To do this, in a terminal window type:

Code: Select all

sudo mount /dev/sda2 /media

Now cd to /media and do an ls. You should see something like this:

Code: Select all

pi@RPi3BP:~ $ cd /media
pi@RPi3BP:/media $ ls
bin   debootstrap  etc   lib         media  opt   root  sbin  sys  usr
boot  dev          home  lost+found  mnt    proc  run   srv   tmp  var

Now cd to home.

Your files should be there and you should be able to copy and/or edit your original files.

You can try removing xx.py and removing the autostart line.
You can then try your old sd card again. If it works, you are on your way. If not, you should still have access to your original files that you copied on to the new sd card.

Return to “Python”