Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different behaviour between python console and python script

I am experiencing different behaviour on the same code using the python console and a python script.

The code is as follows:

import gtk
import webkit
win = gtk.Window()
win.show()
web = webkit.WebView()
win.add(web)
web.show()
web.open("http://www.google.com")

When running the code in the python console, the output is a new frame that contains the google main page.

When running the code as a script, the result is a void frame. It closes very fast but even if I use a delay function, the webkit is not added to the frame.

How is it possible?

Furthermore, using PyDev IDE it flags: "unresolved import: gtk", but if i run the project, the program starts without problem of compilation. is it normal?

like image 389
Luca Avatar asked Nov 24 '12 13:11

Luca


People also ask

What is the console for Python?

The Python Console is a quick way to execute commands, with access to the entire Python API, command history and auto-complete. The command prompt is typical for Python 3. x, the interpreter is loaded and is ready to accept commands at the prompt >>> .

How to execute Python script in Python shell?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!


1 Answers

Add

gtk.main()

to the end of your script. This starts the gtk event loop.


import gtk
import webkit

class App(object):
    def __init__(self):
        win = gtk.Window()
        win.connect("destroy", self.destroy)
        web = webkit.WebView()
        web.open("http://www.google.com")
        win.add(web)
        web.show()
        win.show()
    def destroy(self, widget, data = None):
        gtk.main_quit()
app = App()
gtk.main()
like image 63
unutbu Avatar answered Sep 23 '22 19:09

unutbu