tkinter/lingvigilo.py
author viric <viriketo@gmail.com>
Mon, 03 Sep 2012 23:35:31 +0200
branchgo
changeset 7 d5750f4e6e8c
parent 0 6371497b4e53
permissions -rwxr-xr-x
Adding a first attempt at go code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     1
#!/usr/bin/python
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     3
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     4
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     5
from Tkinter import *
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     6
from tkSimpleDialog import *
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     7
import sys,time,random,string,MySQLdb
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     8
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
     9
# Configuració
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    10
legi_el = 'mysql'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    11
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    12
# Conf - mysql
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    13
database = 'lingvigilo'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    14
dbuser = 'lingvigilouser'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    15
dbpass = 'carrinclo'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    16
dbtable_vortoj = 'vortoj_ru_ca'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    17
dbtable_frazoj = 'frazoj_ru_ca'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    18
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    19
# Conf - fitxer
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    20
dbasefile = 'vortoj-ru-ca.txt'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    21
frazodosiero = 'frazoj.txt'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    22
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    23
# Prendre les paraules d'una base de dades MySQL
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    24
def getMySQL():
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    25
	db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    26
		db=database)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    27
	cursor = db.cursor()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    28
	cursor.execute("SELECT * FROM "+dbtable_vortoj)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    29
	res_vortoj = cursor.fetchall()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    30
	res_vortoj_unicode = []
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    31
	for vorto in res_vortoj:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    32
		vorto_vorto = unicode(vorto[1],'utf8')
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    33
		vorto_tipo = unicode(vorto[2],'utf8')
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    34
		vorto_traduko = unicode(vorto[3],'utf8')
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    35
		
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    36
		# Paràmetres opcionals
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    37
		if vorto[4] == None:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    38
			vorto_param1 = ''
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    39
		else:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    40
			vorto_param1 = unicode(vorto[4],'utf8')
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    41
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    42
		if vorto[5] == None:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    43
			vorto_param2 = ''
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    44
		else:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    45
			vorto_param2 = unicode(vorto[5],'utf8')
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    46
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    47
		res_vortoj_unicode.append(
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    48
			(vorto_vorto, vorto_tipo, vorto_traduko,
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    49
			vorto_param1, vorto_param2))
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    50
	db.close()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    51
	return res_vortoj_unicode
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    52
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    53
# Prendre les paraules d'un fitxer
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    54
def getFile():
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    55
	dbase = open(dbasefile,"r")
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    56
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    57
	lines = dbase.readlines()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    58
	dbase.close()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    59
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    60
	res_vortoj = []
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    61
	for line in lines:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    62
		line = line.rstrip('\n')
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    63
		vortoj = string.split(unicode(line.rstrip('\n'),'utf8'),'\t')
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    64
		res_vortoj.append(vortoj)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    65
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    66
	return res_vortoj
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    67
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    68
if legi_el == 'file':
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    69
	print "Legante vortojn el dosiero..."
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    70
	vortoj = getFile()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    71
else:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    72
	print "Legante vortojn el MySQLa datumbazo..."
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    73
	vortoj = getMySQL()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    74
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    75
print "Legitaj", len(vortoj), "vortoj."
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    76
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    77
def AldoniFrazonDosieren(vorto,teksto):
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    78
	outfile = open(frazodosiero,"a")
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    79
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    80
	lineout = vorto + "\t" + teksto +"\n"
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    81
	outfile.write(lineout.encode('utf8'))
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    82
	
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    83
	outfile.close()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    84
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    85
def AldoniFrazonDatumbazen(vorto,teksto):
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    86
	db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    87
		db=database)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    88
	cursor = db.cursor()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    89
	print ("INSERT INTO "+dbtable_frazoj+" (vorto, frazo)"
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    90
		" VALUES('%s', '%s');" % (vorto, teksto))
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    91
	cursor.execute("INSERT INTO "+dbtable_frazoj+" (vorto, frazo)"+
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    92
		" VALUES('%s', '%s');" % (vorto.encode('utf8'),
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    93
		teksto.encode('utf8')))
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    94
	db.close()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    95
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    96
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    97
def LoopFunction():
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    98
	index = random.randint(0, len(vortoj)-1)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
    99
	vorto = vortoj[index][0]
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   100
	teksto = askstring("Frazilon", "Skribu frazon por vorto '%s'" % vorto)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   101
	if teksto == None:
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   102
		print 'Fi del programa'
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   103
		sys.exit()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   104
	AldoniFrazonDatumbazen(vorto, teksto)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   105
	root.after(1*1000,LoopFunction)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   106
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   107
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   108
# Main program
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   109
root=Tk()
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   110
root.after(10,LoopFunction)
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   111
Message(root, text='Programo por lerni lingvon').pack();
6371497b4e53 Init from svn. The wxpython is the final one.
viric@llimona
parents:
diff changeset
   112
root.mainloop()