SmallPythonProjects/GTK-examples/ReadAndApplyCsv.py
2021-07-01 14:17:59 +02:00

160 lines
4.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 20 09:44:07 2021
@author: angoosh
"""
import threading
#from multiprocessing import Process, Queue
import queue
import gi
from time import sleep
gi.require_version('Gtk', '3.0')
gi.require_version('GLib', '2.0')
from gi.repository import Gtk, GLib
IsEnded = 0
rows = []
csvContent = []
csvFile = ""
def do_work():
global csvContent
while True:
with open(csvFile,"r") as file:
content = file.read()
print(content)
lines = content.split("\n")
csvContent = lines
file.close()
sleep(1)
if IsEnded != 0:
break
class MainGUI(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Append")
self.com_queue = queue.Queue()
self.worker_thread = None
self.liststore = None
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.vbox)
self.box = Gtk.Box(spacing=6)
self.vbox.pack_start(self.box, True, True, 0)
self.textArea = Gtk.Entry()
self.textArea.set_text("test.csv")
self.box.pack_start(self.textArea, True, True, 0)
self.button1 = Gtk.Button(label="Load")
self.button1.connect("clicked", self.addRow)
self.box.pack_start(self.button1, True, True, 0)
self.button2 = Gtk.Button(label="Update")
self.button2.connect("clicked", self.ApplyCsv)
self.box.pack_start(self.button2, True, True, 0)
self.nbox = Gtk.Grid()
self.vbox.pack_start(self.nbox, True, True, 0)
self.tid = None
self.proc = None
def launch_worker_thread(self):
self.worker_thread = threading.Thread(target=do_work)
self.worker_thread.start()
def butt(self, widget):
global rows
print(rows)
for row in rows:
row[3].set_text("3")
def addRow(self, widget):
global rows, csvContent, csvFile
iteration = 0
csvFile = self.textArea.get_text()
with open(self.textArea.get_text(),"r") as file:
content = file.read()
print(content)
lines = content.split("\n")
csvContent = lines
file.close()
for line in lines:
try:
inLine = line.split(",")
print(inLine)
varType = inLine[0]
varName = inLine[1]
value = inLine[2]
typeLabel = Gtk.Label(label=varType, halign=Gtk.Align.START)
f = Gtk.Frame()
f.add(typeLabel)
self.nbox.attach(f,0,iteration,1,1)
nameLabel = Gtk.Label(label=varName+":", halign=Gtk.Align.END)
f = Gtk.Frame()
f.add(nameLabel)
self.nbox.attach(f,1,iteration,1,1)
#nlabel = Gtk.Label(label=varType+" "+varName+":")
#nbox.pack_start(nlabel, True, True, 0)
nentry = Gtk.Entry()
nentry.set_hexpand(False)
nentry.set_text(value)
nentry.connect("activate", self.printContent, varType, varName,)
self.nbox.attach(nentry,2,iteration,3,1)
#nbox.pack_start(nentry, True, True, 0)
self.nbox.show_all()
#rowContent = [len(rows), nlabel, nentry]
rowContent = [len(rows),typeLabel, nameLabel, nentry]
rows.append(rowContent)
iteration += 1
except:
pass
self.launch_worker_thread()
def printContent(self, widget, vt, vn):
print(vt+" "+vn+": "+widget.get_text())
def ApplyCsv(self, widget):
global csvContent
values = []
iteration = 0
for content in csvContent:
try:
bits = content.split(",")
values.append(bits[2])
except:
pass
for row in rows:
row[3].set_text(values[iteration])
iteration += 1
def butt2(self, widget):
global IsEnded
IsEnded = 1
def prn(self, ignored):
print("B")
GLib.idle_add(self.update_treeview, "update")
if __name__ == "__main__":
gui = MainGUI()
gui.connect("destroy", Gtk.main_quit)
gui.show_all()
Gtk.main()
IsEnded = 1