[python] WxPython :: Problém s vláknem

Jakub Vojáček Jakohv na seznam.cz
Středa Duben 16 22:06:58 CEST 2008


Ahoj

Děkuji, Váš kód mě navedl na jedno řešení:)
To druhé okno už konečně nezamrzává. 

#-*- coding: utf-8 -*-
import re, time
import thread,threading, socket,wx
from Tkinter import*
DEFAULT_HOST='localhost'
DEFAULT_PORT=1991
class ReceiveEmail:
    def __init__(self):
        while 1:
            self.send_warning("some warning")
            time.sleep(15)  
    def send_warning(self,what):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((DEFAULT_HOST, DEFAULT_PORT))
        s.send(what)
        s.close()
class Server(threading.Thread):
    def __init__(self,prijimaci_funkce, okno2):
        self.okno2=okno2
        self.prijimaci_funkce=prijimaci_funkce
        self.S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.S.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.S.bind((DEFAULT_HOST,DEFAULT_PORT))
        self.S.listen(5)
        threading.Thread.__init__(self)
        self.start()
        #Start thread:
        thread.start_new_thread(ReceiveEmail, ())
    def run(self):
        while True:
            try:
                newS, address = self.S.accept()
                receivedData = newS.recv(1024)
                self.prijimaci_funkce(receivedData, self.okno2)
                newS.close()
            finally: pass
        self.S.close()
class MyTimer(wx.Timer):
    def Notify(self):
        self.window.Hide()
        self.Stop()
class EmailWarningWindow:
    def __init__(self):
        self.okno=wx.Frame(None,title="Second Window",id=1987,size=(490,340),style =wx.SIMPLE_BORDER|wx.STAY_ON_TOP)
        wx.StaticText(self.okno, label="some text")  
        self.okno.SetAutoLayout(True)
    def ukaz(self, data):
        #zpracovani dat
        self.okno.Show()
class App:
    def __init__(self):
        okno = wx.App(0)
        self.otevreno=False
        self.okno=wx.Frame(None,title="First window",id=-1,size=(490,340))
        self.okno2=EmailWarningWindow()
        self.okno2.okno.Hide()
        Server(accept, self.okno2)
        self.okno.Show(True)
        okno.MainLoop()
def accept(data, okno):
    #Run the second window; 
    okno.ukaz(data)
    thread.start_new_thread(lambda:zavri(okno.okno.Hide), ())
def zavri(x):
    time.sleep(3)
    x()
if __name__ == "__main__":
    App()




> ------------ Původní zpráva ------------
> Od: Petr Nesladek <petr.nesladek na seznam.cz>
> Předmět: Re: [python] WxPython :: Problém s vláknem
> Datum: 16.4.2008 20:20:38
> ----------------------------------------
> Ahoj,
> 
> mám pocit, že to není  v wx - ale kříží se tam thread s vnitřními událostmi wx -
> zkus se podívat na tuto úpravu - 
> každou změnu jsem komentoval 
> 
> pene
> 
> 
> import re, time
> import thread,threading, socket,wx
> DEFAULT_HOST='localhost'
> DEFAULT_PORT=1991
> class ReceiveEmail:
>     def __init__(self):
>         while 1:
>             self.send_warning("some warning")
>             time.sleep(15)  
>     def send_warning(self,what):
>         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>         s.connect((DEFAULT_HOST, DEFAULT_PORT))
>         s.send(what)
>         s.close()
> class Server(threading.Thread):
>     def __init__(self,prijimaci_funkce):
>         self.prijimaci_funkce=prijimaci_funkce
>         self.S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>         self.S.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>         self.S.bind((DEFAULT_HOST,DEFAULT_PORT))
>         self.S.listen(5)
>         threading.Thread.__init__(self)
>         self.start()
>         #Start thread:
>         thread.start_new_thread(ReceiveEmail, ())
>     def run(self):
>         while True:
>             try:
>                 newS, address = self.S.accept()
>                 receivedData = newS.recv(1024)
>                 self.prijimaci_funkce(receivedData)
>                 newS.close()
>             finally: pass
>         self.S.close()
> class EmailWarningWindow:
>     def __init__(self):
>         self.okno=wx.Frame(None,title="Second
> Window",id=1987,size=(490,340))#,style =wx.SIMPLE_BORDER|wx.STAY_ON_TOP)
>         wx.StaticText(self.okno, label="some text")
>         wx.Yield()
>         self.okno.SetAutoLayout(True)
>         self.okno.Show(True)
> 
> #       pridany TextCtrl  -pro zapis adt posilanych na EmailWarningWindow
>         self.tx = wx.TextCtrl(self.okno, -1,"", size=(200, 100),
> style=wx.TE_MULTILINE)
>         self.tx.SetInsertionPointEnd()
> #   pridana metoda write
>     def write( self, data):
>         self.tx.AppendText(data)
>         
> class App:
>     def __init__(self):
>         okno = wx.App(0)
>         self.okno=wx.Frame(None,title="First window",id=-1,size=(490,340))
> #
>         self.okno2 = EmailWarningWindow()
> 
>         Server(self.accept)
>         self.okno.Show(True)
>         
>         okno.MainLoop()
> 
> #   definovane jako metoda App        
>     def accept(self, data):
>         #Run the second window; 
>         
>         #EmailWarningWindow()
> #       volani pridane metody write objektu EmailWarningWindow
>         self.okno2.write(data)
>     
> if __name__ == "__main__":
>     App()
> _______________________________________________
> Python mailing list
> Python na py.cz
> http://www.py.cz/mailman/listinfo/python
> 
> 
> 

Blujacker


Další informace o konferenci Python