Update Gui testing

This commit is contained in:
Lerking
2018-02-27 13:18:20 +01:00
committed by GitHub
parent 1a978407c0
commit 4280264fa6
11 changed files with 1355 additions and 60 deletions

82
Gui test/gtk_test_2.py Normal file
View File

@@ -0,0 +1,82 @@
#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class FileChooserWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="FileChooser Example")
box = Gtk.Box(spacing=6)
self.add(box)
button1 = Gtk.Button("Choose File")
button1.connect("clicked", self.on_file_clicked)
box.add(button1)
button2 = Gtk.Button("Choose Folder")
button2.connect("clicked", self.on_folder_clicked)
box.add(button2)
def on_file_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a file", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
def add_filters(self, dialog):
filter_text = Gtk.FileFilter()
filter_text.set_name("Text files")
filter_text.add_mime_type("text/plain")
dialog.add_filter(filter_text)
filter_py = Gtk.FileFilter()
filter_py.set_name("Python files")
filter_py.add_mime_type("text/x-python")
dialog.add_filter(filter_py)
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dialog.add_filter(filter_any)
def on_folder_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a folder", self,
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
"Select", Gtk.ResponseType.OK))
dialog.set_default_size(800, 400)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Select clicked")
print("Folder selected: " + dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
win = FileChooserWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

80
Gui test/gtk_test_3.py Normal file
View File

@@ -0,0 +1,80 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import time
from multiprocessing import Process, Value, Lock, Pool
class Counter(object):
def __init__(self, initval=0.0):
self.val = Value('d', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1/2542
print(self.val.value)
def value(self):
with self.lock:
return self.val.value
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title = 'Hello World')
#self.val = 0
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.button1 = Gtk.Button(label = 'Hello')
self.button1.connect('clicked', self.on_button1_clicked)
self.box.pack_start(self.button1, True, True, 0)
self.button2 = Gtk.Button(label = 'Goodbye')
self.button2.connect('clicked', self.on_button2_clicked)
self.box.pack_start(self.button2, True, True, 0)
self.pbar = Gtk.ProgressBar()
self.pbar.set_fraction(0.0)
self.box.pack_start(self.pbar, True, True, 0)
def on_button1_clicked(self, widget):
print('Hello')
#procs = [Process(target=self.func, args=(counter,)) for i in range(10)]
#for p in procs: p.start()
#for p in procs: p.join()
p = Pool(2)
p.map(self.func, range(2542))
print (counter.value())
#while counter.val.value <10:
#self.update_pbar(self.pbar)
print('Goodbye')
Gtk.main_quit()
def on_button2_clicked(self, widget):
print('Goodbye')
Gtk.main_quit()
def update_pbar(self, widget, value):
#self.val += 0.1
self.pbar.set_fraction(value)
#time.sleep(0.5)
while Gtk.events_pending():
Gtk.main_iteration()
def func(self, data):
#for i in range(2542):
time.sleep(0.01)
counter.increment()
#while counter.value() <1:
#self.update_pbar(self.pbar, counter.value())
counter = Counter(0.0)
win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

118
Gui test/gtk_test_4.py Normal file
View File

@@ -0,0 +1,118 @@
import gobject
import pygtk
pygtk.require('2.0')
import gtk
import multiprocessing
import threading
import time
gtk.gdk.threads_init()
class Listener(gobject.GObject):
__gsignals__ = {
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT, gobject.TYPE_STRING)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, queue):
gobject.GObject.__init__(self)
self.queue = queue
def go(self):
print("Listener has started")
while True:
# Listen for results on the queue and process them accordingly
data = self.queue.get()
# Check if finished
if data[1]=="finished":
print("Listener is finishing.")
self.emit("finished")
return
else:
self.emit('updated', data[0], data[1])
gobject.type_register(Listener)
class Worker():
def __init__(self, queue):
self.queue = queue
def go(self):
print("The worker has started doing some work (counting from 0 to 9)")
for i in range(10):
proportion = (float(i+1))/10
self.queue.put((proportion, "working..."))
time.sleep(0.5)
self.queue.put((1.0, "finished"))
print("The worker has finished.")
class Interface:
def __init__(self):
self.process = None
self.progress = gtk.ProgressBar()
button = gtk.Button("Go!")
button.connect("clicked", self.go)
vbox = gtk.VBox(spacing=5)
vbox.pack_start(self.progress)
vbox.pack_start(button)
vbox.show_all()
self.frame = vbox
def main(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_border_width(10)
window.add(self.frame)
window.show()
window.connect("destroy", self.destroy)
gtk.main()
def destroy(self, widget, data=None):
gtk.main_quit()
def callbackDisplay(self, obj, fraction, text, data=None):
self.progress.set_fraction(fraction)
self.progress.set_text(text)
def callbackFinished(self, obj, data=None):
if self.process==None:
raise RuntimeError("No worker process started")
print("all done; joining worker process")
self.process.join()
self.process = None
self.progress.set_fraction(1.0)
self.progress.set_text("done")
def go(self, widget, data=None):
if self.process!=None:
return
print("Creating shared Queue")
queue = multiprocessing.Queue()
print("Creating Worker")
worker = Worker(queue)
print("Creating Listener")
listener = Listener(queue)
listener.connect("updated",self.callbackDisplay)
listener.connect("finished",self.callbackFinished)
print("Starting Worker")
self.process = multiprocessing.Process(target=worker.go, args=())
self.process.start()
print("Starting Listener")
thread = threading.Thread(target=listener.go, args=())
thread.start()
if __name__ == '__main__':
gui = Interface()
gui.main()

166
Gui test/gtk_test_5.py Normal file
View File

@@ -0,0 +1,166 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject as gobject
import time
from multiprocessing import Process, Value, Lock, Pool, Queue
import threading
gobject.threads_init()
class Listener(gobject.GObject):
__gsignals__ = {
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT, gobject.TYPE_STRING)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, queue):
gobject.GObject.__init__(self)
self.queue = queue
def go(self):
print("Listener has started")
while True:
# Listen for results on the queue and process them accordingly
data = self.queue.get()
# Check if finished
if data[1]=="finished":
print("Listener is finishing.")
self.emit("finished")
return
else:
self.emit('updated', data[0], data[1])
gobject.type_register(Listener)
class Worker():
def __init__(self, queue):
self.queue = queue
def go(self):
print("The worker has started doing some work (counting from 0 to 9)")
for i in range(2542):
proportion = (float(i+1))/2542
self.queue.put((proportion, "working..."))
time.sleep(0.01)
self.queue.put((1.0, "finished"))
print("The worker has finished.")
class Counter(object):
def __init__(self, initval=0.0):
self.val = Value('d', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1/2542
print(self.val.value)
def value(self):
with self.lock:
return self.val.value
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title = 'Hello World')
#self.val = 0
self.process = None
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.button1 = Gtk.Button(label = 'Hello')
self.button1.connect('clicked', self.go)
self.box.pack_start(self.button1, True, True, 0)
self.button2 = Gtk.Button(label = 'Goodbye')
self.button2.connect('clicked', self.on_button2_clicked)
self.box.pack_start(self.button2, True, True, 0)
self.pbar = Gtk.ProgressBar()
self.pbar.set_fraction(0.0)
self.box.pack_start(self.pbar, True, True, 0)
def callbackDisplay(self, obj, fraction, text, data=None):
self.pbar.set_fraction(fraction)
self.pbar.set_text(text)
def callbackFinished(self, obj, data=None):
if self.process==None:
raise RuntimeError("No worker process started")
print("all done; joining worker process")
self.process.join()
self.process = None
self.pbar.set_fraction(1.0)
self.pbar.set_text("done")
print('Goodbye')
Gtk.main_quit()
def go(self, widget, data=None):
if self.process!=None:
return
print("Creating shared Queue")
queue = Queue()
print("Creating Worker")
worker = Worker(queue)
print("Creating Listener")
listener = Listener(queue)
listener.connect("updated",self.callbackDisplay)
listener.connect("finished",self.callbackFinished)
print("Starting Worker")
self.process = Process(target=worker.go, args=())
self.process.start()
print("Starting Listener")
thread = threading.Thread(target=listener.go, args=())
thread.start()
#def on_button1_clicked(self, widget):
#print('Hello')
#procs = [Process(target=self.func, args=(counter,)) for i in range(10)]
#for p in procs: p.start()
#for p in procs: p.join()
#p = Pool(2)
#p.map(self.func, range(2542))
#print (counter.value())
#while counter.val.value <10:
#self.update_pbar(self.pbar)
#print('Goodbye')
#Gtk.main_quit()
def on_button2_clicked(self, widget):
print('Goodbye')
Gtk.main_quit()
#def update_pbar(self, widget, value):
#self.val += 0.1
#self.pbar.set_fraction(value)
#time.sleep(0.5)
#while Gtk.events_pending():
#Gtk.main_iteration()
#def func(self, data):
#for i in range(2542):
#time.sleep(0.01)
#counter.increment()
#while counter.value() <1:
#self.update_pbar(self.pbar, counter.value())
#counter = Counter(0.0)
win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

393
Gui test/h2inc (copy).xml Normal file
View File

@@ -0,0 +1,393 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkFileChooserDialog" id="filechooserdialog1">
<property name="can_focus">False</property>
<property name="type_hint">normal</property>
<property name="action">select-folder</property>
<child internal-child="vbox">
<object class="GtkBox" id="filechooserdialog-vbox1">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="filechooserdialog-action_area1">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="sourceframe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Source:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select source directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Destination:</property>
<property name="justify">right</property>
<property name="ellipsize">start</property>
<property name="angle">2.2351741811588166e-10</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select destination directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Create "include" folder if it does not exist.</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">6</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">5</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Number of folders: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label9">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Total progress:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label8">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Translation</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">5</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

367
Gui test/h2inc.glade~ Normal file
View File

@@ -0,0 +1,367 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkFrame" id="sourceframe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Source:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select source directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Destination:</property>
<property name="justify">right</property>
<property name="ellipsize">start</property>
<property name="angle">2.2351741811588166e-10</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select destination directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Create "include" folder if it does not exist.</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">6</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">5</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Number of folders: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label9">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Total progress:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label8">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Translation</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">5</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

@@ -9,11 +9,58 @@
import os
import sys
import multiprocessing as mp
import io
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GdkPixbuf
from gi.repository import Gtk, Gio, GObject as gobject
import time
from multiprocessing import Process, Value, Lock, Pool, Queue
import threading
gobject.threads_init()
class Listener(gobject.GObject):
__gsignals__ = {
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT, gobject.TYPE_STRING, gobject.TYPE_INT)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, queue):
gobject.GObject.__init__(self)
self.queue = queue
def go(self):
print("Listener has started")
while True:
# Listen for results on the queue and process them accordingly
data = self.queue.get()
# Check if finished
if data[1]=="finished":
print("Listener is finishing.")
self.emit("finished")
return
else:
self.emit('updated', data[0], data[1], data[2])
gobject.type_register(Listener)
class Worker():
def __init__(self, queue, filecnt, filelist):
self.queue = queue
self.filecnt = filecnt
def go(self):
print("The worker has started doing some work (counting from 0 to 9)")
for i in range(self.filecnt):
proportion = (float(i+1))/self.filecnt
self.queue.put((proportion, "working...", i))
process_file(filelist[i])
self.queue.put((1.0, "finished"))
print("The worker has finished.")
tupline = []
preproc = ()
@@ -22,8 +69,9 @@ folderlist = []
cnt = 0
srcdir = ''
destdir = ''
num_cores = mp.cpu_count()
#num_cores = multiprocessing.cpu_count()
fileindex = 0
filecnt = 0
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
@@ -110,12 +158,66 @@ def process_file(data):
newfile.write(outfile)
newfile.close()
def async_process(num):
pool = mp.Pool(processes=num)
pool.map(process_file, filelist)
class ExampleApp:
class Handler:
global app
global destlabel
def __init__(self):
self.filecnt = 0
self.fileindex = 0
self.process = None
self.app = Gtk.Application.new("org.h2inc", Gio.ApplicationFlags(0))
self.app.connect("activate", self.on_app_activate)
self.app.connect("shutdown", self.on_app_shutdown)
app = self.app
def on_app_activate(self, app):
builder = Gtk.Builder()
builder.add_from_file("h2inc.glade")
builder.connect_signals(self)
self.obj = builder.get_object
self.obj("window").set_application(app)
self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk")
self.obj("window").show_all()
button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.CANCEL)
button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.OK)
def on_app_shutdown(self, app):
self.app.quit()
def run(self, argv):
self.app.run(argv)
def callbackDisplay(self, obj, fraction, text, index, data=None):
self.obj("progress_label").set_text("{} of {}".format(index+1, self.filecnt))
self.obj("progressbar").set_fraction(fraction)
self.obj("progressbar").set_text(text)
def callbackFinished(self, obj, data=None):
if self.process==None:
raise RuntimeError("No worker process started")
print("all done; joining worker process")
self.process.join()
self.process = None
self.obj("progressbar").set_fraction(1.0)
self.obj("progressbar").set_text("done")
self.obj("sourceframe").set_sensitive(True)
self.obj("translate_button").set_sensitive(True)
self.obj("destination_label").set_sensitive(True)
self.obj("destination_entry").set_sensitive(True)
self.obj("destination_button").set_sensitive(True)
self.obj("include_checkbutton").set_sensitive(True)
self.obj("translation_frame").set_sensitive(True)
def on_window_destroy(self,window):
window.close()
@@ -130,17 +232,6 @@ class Handler:
print("File selection: %s" % widget.get_filename())
self.on_dialog_close(widget)
def on_filechooser_dialog_file_activated(self,widget):
self.on_filechooser_dialog_response(widget,-5)
def on_filechooser_dialog_update_preview(self,widget):
if widget.get_filename() != None and os.path.isfile(widget.get_filename()):
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(widget.get_filename(),200,200,True)
app.obj("preview").set_from_pixbuf(pixbuf)
def on_file_button_clicked(self,widget):
app.obj("filechooser_dialog").show_all()
def on_source_button_clicked(self,widget):
dialog = Gtk.FileChooserDialog("Select source directory!",
@@ -152,15 +243,15 @@ class Handler:
response = dialog.run()
if response == Gtk.ResponseType.OK:
filecnt = sourcedir_filecnt(dialog.get_filename())
if filecnt >0:
print(filecnt)
self.filecnt = sourcedir_filecnt(dialog.get_filename())
if self.filecnt >0:
print(self.filecnt)
app.obj("source_entry").set_text(dialog.get_filename())
app.obj("destination_label").set_sensitive(True)
app.obj("destination_entry").set_sensitive(True)
app.obj("destination_button").set_sensitive(True)
app.obj("numfiles_label").set_text(str(filecnt))
app.obj("progress_label").set_text("{} of {}".format(fileindex, filecnt))
app.obj("numfiles_label").set_text(str(self.filecnt))
app.obj("progress_label").set_text("{} of {}".format(fileindex, self.filecnt))
foldercnt = sourcedir_foldercnt(dialog.get_filename())
if foldercnt >0:
app.obj("numfolders_label").set_text(str(foldercnt))
@@ -192,51 +283,31 @@ class Handler:
dialog.destroy()
def on_translate_button_clicked(self, widget):
def on_translate_button_clicked(self, widget, data=None):
app.obj("sourceframe").set_sensitive(False)
app.obj("translate_button").set_sensitive(False)
process_files()
def update_pbar(self, widget):
self.val += 1/filecnt
self.pbar.set_fraction(self.val)
while Gtk.events_pending():
Gtk.main_iteration()
class ExampleApp:
global app
global destlabel
def __init__(self):
self.app = Gtk.Application.new("org.h2inc", Gio.ApplicationFlags(0))
self.app.connect("activate", self.on_app_activate)
self.app.connect("shutdown", self.on_app_shutdown)
app = self.app
if self.process!=None:
return
def on_app_activate(self, app):
builder = Gtk.Builder()
builder.add_from_file("h2inc.glade")
builder.connect_signals(Handler())
print("Creating shared Queue")
queue = Queue()
self.obj = builder.get_object
self.obj("window").set_application(app)
self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk")
self.obj("window").show_all()
print("Creating Worker")
worker = Worker(queue, self.filecnt, filelist)
button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.CANCEL)
button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.OK)
print("Creating Listener")
listener = Listener(queue)
listener.connect("updated",self.callbackDisplay)
listener.connect("finished",self.callbackFinished)
def on_app_shutdown(self, app):
self.app.quit()
print("Starting Worker")
self.process = Process(target=worker.go, args=())
self.process.start()
def run(self, argv):
self.app.run(argv)
print("Starting Listener")
thread = threading.Thread(target=listener.go, args=())
thread.start()
app = ExampleApp()
app.run(sys.argv)

View File

@@ -0,0 +1,18 @@
from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
def f(name):
info('function f')
print('hello', name)
if __name__ == '__main__':
info('main line')
p = Process(target=f, args=('bob',))
p.start()
p.join()

Binary file not shown.

Binary file not shown.

Binary file not shown.