jayanthony13
Posts: 10
Joined: Sat Feb 20, 2016 11:40 am

test script: using python-tkinter

Wed Mar 16, 2016 1:24 pm

Hi,

Just for curiosity I have had copied the test script (see below) from one of the online tutorial and I want to see if the test script will work on the raspberry pi. I was surprised when I run it through the raspberry pi it did not work. I have had used the python idle 3.4.4. But when I run it through my windows laptop with 64 bits and python 3 with 3.5.1 it works ok.

from Tkinter import *
# if you are working under Python 3, comment the previous line and comment out the following line
#from tkinter import *

root = Tk()

w = Label(root, text="Hello Tkinter!")
w.pack()

root.mainloop()


Any comments and suggestions on this matter is much appreciated!

Many thanks in advance.

scotty101
Posts: 3720
Joined: Fri Jun 08, 2012 6:03 pm

Re: test script: using python-tkinter

Wed Mar 16, 2016 1:44 pm

I've written quite a few things with Tkinter both on Windows and Raspbian and have never had any issues.

What error message(s) do you get?

(PS please post your code in side [code][/code] tags in future so that it retains the white space)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

DirkS
Posts: 9941
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: test script: using python-tkinter

Wed Mar 16, 2016 1:47 pm

I assume you get an import error?
# if you are working under Python 3, comment the previous line and comment out the following line
Translation:
if you are running (or using) Python 3 , comment out the previous line and uncomment the following line
You're running Idle 3, so you need to use the second form:

Code: Select all

from tkinter import *

jayanthony13
Posts: 10
Joined: Sat Feb 20, 2016 11:40 am

Re: test script: using python-tkinter

Wed Mar 16, 2016 2:58 pm

scotty101 wrote:I've written quite a few things with Tkinter both on Windows and Raspbian and have never had any issues.

What error message(s) do you get?

(PS please post your code in side tags in future so that it retains the white space)
Hi Scotty,

thanks for replying back. I am sorry I don't know where is the code tags. To be honest I am beginner and still learning on this forum as well. Anyway, the error I've got is:

Traceback (most recent cal last):
File "/home/pi/hello2.py, line 5, in <module>
w = label (root, text = "Hello Tkinter!")
NameError: name 'label' is not defined

DirkS
Posts: 9941
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: test script: using python-tkinter

Wed Mar 16, 2016 3:01 pm

jayanthony13 wrote:thanks for replying back. I am sorry I don't know where is the code tags.
Should be somewhere at the top when you're editing a post.
Traceback (most recent cal last):
File "/home/pi/hello2.py, line 5, in <module>
w = label (root, text = "Hello Tkinter!")
NameError: name 'label' is not defined
That code is different from your top post.
I think it should be 'Label' instead of 'label'

Python is case sensitive (and even more sensitive to indentation)

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

Re: test script: using python-tkinter

Wed Mar 16, 2016 3:12 pm

Tkinter requires some study, The best documentation I know is
http://infohost.nmt.edu/tcc/help/pubs/t ... index.html

And here is a simple application example
http://infohost.nmt.edu/tcc/help/pubs/t ... l-app.html
explained step by step.
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

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: test script: using python-tkinter

Wed Mar 16, 2016 3:37 pm

Welcome to the womderful world of tkinter.
gkreidl wrote:Tkinter requires some study, The best documentation I know is
http://infohost.nmt.edu/tcc/help/pubs/t ... index.html
I agree this is the best but it is not complete so sometims you need to resort to Google
And here is a simple application example
http://infohost.nmt.edu/tcc/help/pubs/t ... l-app.html
explained step by step.
This certainly works but you will find many styles of writing. For instance:

Code: Select all

import tkinter as tk

main_window = tk.Tk()
tk.Label(main_window,text = 'Hello world').pack()
main_window.mainloop()
and

Code: Select all

import tkinter as tk

class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        
        label = tk.Label(self,text = 'Hello world')
        label.pack()
        
Application().mainloop()
produce the same result in different ways. You will need to develop your own prefered style.

Return to “Python”