simple_gtk.py :  » Development » Python-Twain-Module » twain_1.0.3 » demo » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Python Twain Module 
Python Twain Module » twain_1.0.3 » demo » simple_gtk.py
"""simple_gtk.py

This is a simple demonstration of the twain module using pyGTK.

This version does uses callbacks. Polling can be used instead by
modifying the global variable USE_CALLBACK below.

To use callbacks with pyGTK, you must use twainmodule 1.0.3 or later.
"""

import pygtk
pygtk.require('2.0')
import gtk
import gobject

from simple_base import TwainBase

# You can either Poll the TWAIN source, or process the scanned image in an
# event callback. The event callback has not been fully tested using GTK.
# Specifically this does not work with Tkinter.
USE_CALLBACK=True


class ApplicationWindow(TwainBase):

    ui = '''<ui>
    <menubar name="MenuBar">
      <menu action="TWAIN">
        <menuitem action="Open Scanner"/>
        <menuitem action="Acquire By File"/>
        <menuitem action="Acquire Natively"/>
        <menuitem action="Quit"/>
      </menu>
    </menubar>
    </ui>'''


    def mnuOpenScanner(self, widget=None, event=None, data=None):
        """Connect to the scanner"""
        self.OpenScanner(self.window.window.handle,
            ProductName="Simple pyGTK Demo", UseCallback=USE_CALLBACK)
        return True
    
    def mnuAcquireNatively(self, widget=None, event=None, data=None):
        """Acquire Natively - this is a memory based transfer"""
        return self.AcquireNatively()

    def mnuAcquireByFile(self, widget=None, event=None, data=None):
        """Acquire by file"""
        return self.AcquireByFile()

    def onIdleTimer(self):
        """This is a polling mechanism. Get the image without relying on the callback."""
        self.PollForImage()
        return True

    def DisplayImage(self, ImageFileName):
        """Display the image from a file"""
        self.image.set_from_file(ImageFileName)
        self.image.show()

    def mnuQuit(self, widget=None, event=None, data=None):
        """I want an exit option on the menu. However, I don't know how to do it."""
        return gtk.main_quit()

    def LogMessage(self, title):
        """ Display the title in the window. I use this as a trivial
        trace of the current state of the program"""
        self.window.set_title(title)
        
    def OnQuit(self, event):
        gtk.main_quit()
        
    def __init__(self):
 
        """This is the pyGTK stuff to create the window and menubar"""
 
        # Set up Window
        window = self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(500, 500)
        window.connect("destroy", lambda w: gtk.main_quit())
        self.LogMessage("Twain Demo - pyGTK")
 
        # Set up Widget Container
        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()
      
        # Setup the UI Manager for Menu    
        ui_manager = gtk.UIManager()
        
        # Add Accelerator Group
        accel_group = ui_manager.get_accel_group()
        window.add_accel_group(accel_group)
        
        # Add ActionGroup
        action_group = gtk.ActionGroup('Simple_GTK Actiongroup')
        
        # Create Actions
        action_group.add_actions(
            [
                 ("TWAIN",                None,    "TWAIN",                "<control>T",   None,    None),
                 ("Open Scanner",         None,    "Open Scanner",         "<control>O",   None,    self.mnuOpenScanner),
                 ("Acquire By File",      None,    "Acquire By File",      "<control>F",   None,    self.mnuAcquireByFile),
                 ("Acquire Natively",     None,    "Acquire Natively",     "<control>N",   None,    self.mnuAcquireNatively),
                 ("Quit",                 None,    "Quit",                 "<control>Q",   None,    self.OnQuit)
            ]
        )  
        
        # Attach the ActionGroup
        ui_manager.insert_action_group(action_group, 0)

        # Add a UI Description
        ui_manager.add_ui_from_string(self.ui)

        # Create a menu-bar to hold the menus and add it to our main window
        menubar = ui_manager.get_widget('/MenuBar')
        vbox.pack_start(menubar, False, False, 2)
        menubar.show()

        # Add an Image field to display what is scanned
        self.image = gtk.Image()
        vbox.pack_end(self.image, True, True, 2)
 
        # Display
        window.show()

        # Set up the idle timer. I use this to check to see if an image is ready.
        if not USE_CALLBACK:
            self.idleTimer = gobject.idle_add(self.onIdleTimer)
        

if __name__ == "__main__":
    app = ApplicationWindow()
    gtk.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.