Hello, i need some help with code, i need to be able to print something that function returns then i press button on panel1 it should return text to panel2 textctrl in wxpython. Here is some bit of code.(i just cut code that i think is needed for this)

import wx,searchengines

 class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Simple", size=(900,700))
        self.SetMinSize(self.GetSize())
        self.panel_one = PanelOne(self)
        self.panel_two = PanelTwo(self)
        self.panel_two.Hide()

    def nextwindow(self,event):
        if self.panel_one.IsShown():
            self.panel_one.Hide()
            self.panel_two.Show()
        else:
            self.panel_one.Show()
            self.panel_two.Hide()
        self.Layout()

class PanelOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)

        self.inputArea = wx.TextCtrl(self, size=(550,35), style=wx.ALIGN_LEFT)

        exOneBtn = buttons.GenBitmapTextButton(self, -1, bmp, "See", size=(100,35))
        self.Bind(wx.EVT_BUTTON, self.onbutton, exOneBtn)

        self.engine = searchengines.SearchEngines()

    def onbutton(self, event):
        frame.nextwindow(self)
        item = self.inputArea.GetValue()
        self.b = self.engine.filest(item)
        transferArea.AppendText(self.b)  # This line problematic, how to write that something should be printed in panel2 textctrl window? i tried PanelTwo(self).transferArea.AppendText(self.b) no errors but nothing prints out..



class PanelTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)

        self.transferArea = wx.TextCtrl(self, size=(400,600), style = wx.TE_MULTILINE)

if __name__== "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

i have few questions, how to create object from class PanelTwo if it's possible, i know how, but where to write that line?

Sorry for my noobish questions. I know i need to learn some OOP :)

Thanks

Recommended Answers

All 7 Replies

It's good place and way to create object from panelone paneltwo classes in

if __name__== "__main__":
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.MainLoop()

to do this

if __name__== "__main__":
app = wx.PySimpleApp()
panelone = PanelOne(MyFrame())
paneltwo = PanelTwo(MyFrame())
frame = MyFrame()
frame.Show()
app.MainLoop()

? :) But still text don't appear in TextCtrl in paneltwo..

Shouldn't you use the frame instead of creating new MyFrame for each panel?

Tried this

for it in self.b:
            frame.panel_two().transferArea.AppendText(it)

but i'm getting error

Traceback (most recent call last):
  File "D:\Python27\PagrindinisProgramos.py", line 110, in onbutton
    frame.panel_two().self.transferArea.AppendText(it)
TypeError: 'PanelTwo' object is not callable

What i'm doing wrong??

Finally

for it in self.b:
            frame.panel_two.transferArea.AppendText(it)

works fine.

My next question is this, how to return values from method constantly? because now method returns whole list at the end then method finishes work. So to user, GUI let say freezes and he need to wait while method done with information and then returns whole list immediately. So i need to return one list item at the time here is example of that method

def filest(self,item):
        l1 =[]
        l2 =[]
        try:
            f = urllib.urlopen('blbalbalb'+item).read()
            r = re.compile('blbla')
            q = re.findall(r, f)
            for link in q:
                u = urllib.urlopen(link).read()
                urlas = re.compile('balblablab')
                miau = re.compile('balblablab')
                surasti2 = re.findall(urlas , u)
                miau2 = re.findall(miau, u)

                for i in surasti2:
                    l1.append(i)
                for sh in miau2:
                    pass
            return l1
            
        except Exception:
            return ('Something wrong')

because then i try

for i in surasti2:
                    l1.append(i)
                    return i # here
                for sh in miau2:
                    pass

method returns just first link and then breaks.

Learn about generators and yield statement.

Learn about generators and yield statement.

I don't quite agree with Tony on this. The problem here seems to be that the function filest() takes time to complete, and wether you are using yield statements or not does not change this. When a function is busy with a long task in a gui context, a standard solution is to run the time-consuming code in a different thread: the GUI's main loop can then resume its normal work. An old, perhaps not optimal, but simple solution to implement this is given in this recipe http://code.activestate.com/recipes/82965/. You could try to write similar code and execute filest() in the worker thread, sending the generated data in a queue.

Yes, i thought about threading, because filest() function will be not only function that will be called. Because i will need to receive info from varies sites so its will be filest(), filesb() and etc. Those functions will be executed at same time, and should return needed information to GUI.

To me the threading is bit hard to understand... But still need to learn it and understand it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.