added multi gtk draw area example

This commit is contained in:
maximstewart 2021-03-04 19:45:37 -06:00
parent 37c4c15427
commit 94d1f1376e
2 changed files with 470 additions and 0 deletions

View File

@ -0,0 +1,106 @@
#!/usr/bin/python3
from setproctitle import setproctitle
import gi, os, faulthandler, signal, inspect, gi, cairo
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
from gi.repository import GdkPixbuf as gbuf
class SupportMixin:
def setFile(self):
dialog = gtk.FileChooserDialog(title="Please choose a file", action=gtk.FileChooserAction.OPEN)
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.ResponseType.CANCEL, gtk.STOCK_OPEN, gtk.ResponseType.OK)
response = dialog.run()
if response == gtk.ResponseType.OK:
self.file = dialog.get_filename()
self.setupSurfaceData(self.file)
elif response == gtk.ResponseType.CANCEL:
pass
dialog.destroy()
def setupSurfaceData(self, file):
pb = gbuf.Pixbuf.new_from_file(file)
if pb:
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, pb.get_width(), pb.get_height())
context = cairo.Context(self.surface)
gdk.cairo_set_source_pixbuf(cr=context, pixbuf=pb, pixbuf_x=0, pixbuf_y=0)
context.paint()
self.drawArea.set_size_request(width=pb.get_width(), height=pb.get_height())
self.drawArea.queue_draw()
class Main_Draw_Area(SupportMixin):
def __init__(self, _builder):
self.surface = None
self.file = None
self.drawArea = _builder.get_object("drawArea1")
def setMainFile(self, widget):
self.setFile()
def onDraw1(self, area, context):
if self.surface is not None:
context.set_source_surface(self.surface, 0.0, 0.0)
context.paint()
return False
class File_Select_Draw_Area(SupportMixin):
def __init__(self, _builder):
self.surface = None
self.file = None
self.drawArea = _builder.get_object("drawArea2")
def setPopupFile(self, widget):
self.setFile()
def onDraw2(self, area, context):
if self.surface is not None:
context.set_source_surface(self.surface, 0.0, 0.0)
context.paint()
return False
class Window():
def __init__(self):
setproctitle('double_draw_areas')
SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__)) + "/"
GLADE_FILE = "main.glade"
self.builder = gtk.Builder()
self.builder.add_from_file(SCRIPT_PTH + GLADE_FILE)
handlers = {}
classes = [self, Main_Draw_Area(self.builder), File_Select_Draw_Area(self.builder)]
for c in classes:
methods = None
try:
methods = inspect.getmembers(c, predicate=inspect.ismethod)
handlers.update(methods)
except Exception as e:
pass
self.builder.connect_signals(handlers)
self.fileSelectionPopup = self.builder.get_object("fileSelectionPopup")
window = self.builder.get_object("main_window")
window.connect("delete_event", gtk.main_quit)
window.show()
def openFileSelectionPopup(self, widget):
self.fileSelectionPopup.popup()
if __name__ == '__main__':
faulthandler.enable() # For better debug info
window = Window()
gtk.main()

View File

@ -0,0 +1,364 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="main_window">
<property name="can-focus">False</property>
<property name="default-width">800</property>
<property name="default-height">800</property>
<child>
<!-- n-columns=10 n-rows=10 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="row-homogeneous">True</property>
<property name="column-homogeneous">True</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">10</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">9</property>
<property name="width">10</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="height">8</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">9</property>
<property name="top-attach">1</property>
<property name="height">8</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<property name="min-content-width">600</property>
<property name="min-content-height">600</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkDrawingArea" id="drawArea1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<signal name="draw" handler="onDraw1" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">1</property>
<property name="width">6</property>
<property name="height">8</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label">Open</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="setMainFile" swapped="no"/>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="openFileSelection">
<property name="label" translatable="yes">File Selection </property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="openFileSelectionPopup" swapped="no"/>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
<object class="GtkPopover" id="fileSelectionPopup">
<property name="width-request">500</property>
<property name="height-request">500</property>
<property name="can-focus">False</property>
<property name="relative-to">openFileSelection</property>
<property name="position">right</property>
<child>
<!-- n-columns=10 n-rows=10 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="row-homogeneous">True</property>
<property name="column-homogeneous">True</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="height">10</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">9</property>
<property name="top-attach">0</property>
<property name="height">10</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
<property name="width">8</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">9</property>
<property name="width">8</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkDrawingArea" id="drawArea2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<signal name="draw" handler="onDraw2" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">3</property>
<property name="width">6</property>
<property name="height">6</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label">Select File</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="setPopupFile" swapped="no"/>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
<property name="width">2</property>
<property name="height">2</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>