Redfez
Posts: 18
Joined: Tue Feb 03, 2015 2:19 am

Differences between Rasp Pi + Python and others?

Wed Feb 11, 2015 3:53 am

Asking for a friend. Bit of a head scratcher. They are using the Pi to try to learn Python (using Python 3 on the Raspi booted into the GUI). As such they've been attempting basic learner code examples from books. Unfortunately it's been failure after failure and I'm taking pity on them and their efforts. Having a look at the book they've been using I couldn't see any obvious typos so I tried it out on a Windows based system with Python IDLE 3.4. They'd attempted several examples, all based around making windows pop up, adding buttons and so on. I thought I'd try the most basic and see if that works and build up from there and that's when I came across the oddity.

The following code (used in the book too):

Code: Select all

from tkinter import *
window = Tk()
Works as expected in Windows and pops up the blank window. No surprise. On the RPi though it does nothing. No window pops up. A blank prompt after the RESTART line and that's it. No surprise then that all the other coding examples in the book involving windows popping up in the later parts of the chapter fail to produce windows too, such as the more detailed interaction and adding buttons and so on.

I thought to double check if the Tkinter library was installed via "sudo apt-get install python-tk" and the same for python3-tk and get the reply that it is already the newest version. I suspected that'd be fine as no error about non-existent libraries appeared either.

Any ideas to save my friend pulling their hair out? Obviously missing something here and it's probably something simple, knowing how these things go. :)

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Differences between Rasp Pi + Python and others?

Wed Feb 11, 2015 4:13 am

I'm not sure why your example works in Windows - assuming that it does, because I cannot check. It does not work on my Linux desktop. The usual minimal example would require that you start the main loop:

Code: Select all

from tkinter import *
window = Tk()
window.mainloop()
That should work.

Redfez
Posts: 18
Joined: Tue Feb 03, 2015 2:19 am

Re: Differences between Rasp Pi + Python and others?

Wed Feb 11, 2015 4:57 pm

Sleep Mode zZ wrote:I'm not sure why your example works in Windows - assuming that it does, because I cannot check. It does not work on my Linux desktop. The usual minimal example would require that you start the main loop:

Code: Select all

from tkinter import *
window = Tk()
window.mainloop()
That should work.
Thanks, I tried that and it works in Windows and the Pi. It works on Windows without the main loop for some reason* and the book doesn't suggest adding that line which seems odd then if it is needed. Not sure what is going on then or what the author was intending.

*tested it again and just the first two lines are enough there - does the system on Windows somehow assume I goofed and add that invisibly while running or something to that effect when running it from IDLE interface?

gkreidl
Posts: 6326
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Differences between Rasp Pi + Python and others?

Wed Feb 11, 2015 5:20 pm

Idle is a Tkinter app and is running a Tkinter mainloop already; perhaps that's the explanation.

If you start your Python app on Windows from the command line (without the mainloop call), it should not run either.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

Redfez
Posts: 18
Joined: Tue Feb 03, 2015 2:19 am

Re: Differences between Rasp Pi + Python and others?

Thu Feb 12, 2015 12:09 am

gkreidl wrote:Idle is a Tkinter app and is running a Tkinter mainloop already; perhaps that's the explanation.

If you start your Python app on Windows from the command line (without the mainloop call), it should not run either.
I tried running it via command line on windows by choosing the .py file and indeed it does nothing without the main loop call but does function with it, which means you may be on to something there. Wonder why this quirk doesn't happen with the Pi version and why does the book not suggest a main loop call? Are they only running their example code with Windows IDLE and never testing them otherwise?? :?

The book that has been causing these woes is called "Beginner's Guide To Computer Coding" with the sub heading "Learn to code in Scratch and Python". It is currently sold in WHSmith next to the Raspberry Pi magazines. Not impressed if this thing is full of nonsense. :(

I've attached a photo of the example in the book. Ignore the handwritten note my friend has added after advice from this thread. You can see that the instructor or author seems to suggest it will work without the main loop call! The other code added further on in this chapter also seems not to function, perhaps due to similarly missing important parts. Perhaps a warning should be given to other potential students or learners on the Pi to avoid this text if it is being pushed on sale right now in shops?
Attachments
window example.JPG
Sample code
window example.JPG (59.92 KiB) Viewed 1081 times

gkreidl
Posts: 6326
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Differences between Rasp Pi + Python and others?

Thu Feb 12, 2015 1:34 am

Tkinter is not easy to use and you need a good reference. The best one I know is here:
http://infohost.nmt.edu/tcc/help/pubs/t ... index.html
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Differences between Rasp Pi + Python and others?

Fri Feb 13, 2015 3:12 pm

The only excuse for the writer might be if he/she has specifically instructed using IDLE...

Which book is that?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”