P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

A really basic tkinter question

Sat Feb 21, 2015 4:50 pm

Hi,
This probably is really basic, but here goes...
I'm trying to learn python by reading books and checking online resources and doing OK so far. My next step is to put a small graphical wrapper around what I'm doing.
So far, I've got this:

Code: Select all

#!/usr/bin/env python

#Import anything necessary
from tkinter import *

#define some functions
def OKaction():
        print('Pressed OK')
def Quitaction():
        print('Pressed Quit')

window = Tk()
window.title("Control Panel")
buttonOK=Button(window, text="OK", command = OKaction)
buttonOK.pack()
buttonQuit=Button(window, text="Quit", command = Quitaction)
buttonQuit.pack()



mainloop()
I'm editing this in an IDLE3 window. If I hit 'run' from the window, the code behaves as expected. If I bring up a text terminal and type 'python filename.py' I get an error that there's no module called tkinter.

can someone tell me what's happening and how toi fix it so I can run my code from the command line?

Thanks

Paul

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

Re: A really basic tkinter question

Sat Feb 21, 2015 4:55 pm

You're using python3 in IDLE3, but Python 2 on the command line. In python 2 the module name is Tkinter.
It's best to stick to one version, so I would use

Code: Select all

python3 filename.py

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: A really basic tkinter question

Sat Feb 21, 2015 4:59 pm

That was quick and easy - thanks :D
I was wondering if it was something like that, but couldn't find a way of telling what version of python I was using on the command line.

Cheers

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: A really basic tkinter question

Sat Feb 21, 2015 6:55 pm

P_Monty wrote:couldn't find a way of telling what version of python I was using on the command line.

Cheers
Too late for you, but for the next person who might need to know

Code: Select all

pi@RPi2B ~ $ python --version
Python 2.7.3
or simply

Code: Select all

pi@RPi2B ~ $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Return to “Python”