Ho solo programmato web fino ad oggi perciò sto faticando molto ad utilizzare e capire il concetto di threading.
codice:
#!/usr/bin/env python
"""
People's Poker Heads-Up SNG HUD
@Author: Tomas Bartoli <***@gmail.com>
@Date: 29/12/2016
@License: Copyright (C) 2016-2017
This software is not open source
"""
# Import general libraries used in the application
import os, sys, re, threading, time
# Import GUI libraries
import pygtk
pygtk.require('2.0')
import gtk
"""
"""
class HUDCore(object):
__rootPath = None
__dataPath = None
__libPath = None
__config = {}
__dbOBj = None
"""
"""
def __init__(self):
self.__initEnvironment()
self.__initDatabase()
self.__initInterface()
# End __init__
"""
" Set environment data
"
" @method private
" @return void
"""
def __initEnvironment(self):
self.__rootPath = sys.path[0] + os.sep
self.__dataPath = sys.path[0] + os.sep + 'data' + os.sep
self.__libPath = sys.path[0] + os.sep + 'libraries' + os.sep
os.chdir(self.__libPath)
sys.path.append(self.__libPath)
# End __initEnvironment
"""
" Connection and selection at the database
"
" @see librariesdatabase.py
" @method private
" @return void
"""
def __initDatabase(self):
file = open(self.__dataPath + 'config.ini', 'r')
while True:
line = file.readline()
if line == '':
break
search = re.findall('([A-Z]{4})s=s"(.*?)"', line)
self.__config[search[0][0]] = search[0][1]
file.close()
import database
self.__dbOBj = database
self.__dbOBj.db(self.__config['HOST'],
self.__config['USER'],
self.__config['PASS'],
self.__config['NAME'])
# End __initDatabase
"""
" Print GUI window
"""
def __initInterface(self):
self.window = gtk.Window(type = gtk.WINDOW_TOPLEVEL)
self.window.set_position(gtk.WIN_POS_CENTER)
self.window.connect('delete_event', self.deleteEvent)
self.window.connect('destroy', self.destroy)
self.window.set_title('People's Poker Heads-Up SNG HUD')
self.window.set_size_request(200, 50)
self.window.set_resizable(False)
hbox = gtk.HBox(True, 10)
hbox.set_border_width(10)
self.window.add(hbox)
hbox.show()
self.BTNImport = gtk.Button('Start Import')
hbox.pack_start(self.BTNImport, True, True, 0)
self.BTNImport.connect('clicked', self.clickImport)
self.BTNImport.show()
# End __initInterface
"""
" @see \__initInterface()
"
" @method private
"
" @param widget
" @param event
" @param data
"
" @return bool False
"""
def deleteEvent(self, widget, event, data = None):
return False
# End deleteEvent
"""
" @see \__initInterface()
"
" @method private
"
" @param widget
" @param data
"
" @return bool False
"""
def destroy(self, widget, data = None):
self.__dbOBj.close()
self.window.destroy()
gtk.main_quit()
# End destroy
"""
"""
def clickImport(self, widget, data = None):
if self.BTNImport.get_label() == 'Start Import':
self.BTNImport.set_label('Stop Import')
var = threading.Thread(target=self.tryThread)
var.setDaemon(True)
var.start()
else:
self.BTNImport.set_label('Start Import')
# End destroy
def tryThread(self):
i = 0
while True:
print str(i)
i = i + 1
time.sleep(5)
"""
"""
def main(self):
self.window.show()
gtk.main()
exit
# End main
# End class HUDCore
if __name__== '__main__':
init = HUDCore()
init.main()
Se provate ad eseguire il codice ci sarà un bottone con scritto "Start Import", cliccandoci sopra vorrei venisse eseguita questa porzione di codice:
codice:
var = threading.Thread(target=self.tryThread)
var.setDaemon(True)
var.start()
# [...]
def tryThread(self):
i = 0
while True:
print str(i)
i = i + 1
time.sleep(5)
e quindi cominciasse a stamparmi in loop dei numeri crescenti ogni 5 secondi, senza però condizionare il funzionamento del programma, non mi stupisce che non funziona, il ciclo viene avviato solo quando chiudo la GUI, perchè?