Newbie help and maybe design issues

Hello and thanks for being here.
I am boB and I am not new to python but I am very new to wxpython. I am trying to do what I thought was a simple project but I am having lots of problems. I am now thinking this may be an initial design issue. I have a frame with a panel as a child. The panel has statictext and textctrls and 3 buttons. I am trying to have the user enter some text to find in a text entry dialog box and then use that to continue. I can get the text but I am stuck there. I am then trying to access the textctrls to put info in them and I can’t. Most of this is going on inside a function that starts the find process. I have some sample code below. Let me know if you need more.
Thanks,

import wx

class wxinfo(wx.Frame):

def __init__(self, parent, title):
    super(wxinfo, self).__init__(parent, title=title, size=(500, 400))

    self.InitUI()

.
.
.
def InitUI(self):
.
.
.
panel = wx.Panel(self)
sizer = wx.GridBagSizer(4, 4)

    text1 = wx.StaticText(panel, label="The Question:")
    sizer.Add(text1, pos=(1, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)

.
.
.
tc1 = wx.TextCtrl(panel)
sizer.Add(tc1, pos=(1, 1), span=(1, 4), flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
.
.
.
button2 = wx.Button(panel, label=“Find Rec”, size=(90, 28))
button2.Bind(wx.EVT_BUTTON, self.OnClickBut2)
.
.
.
def OnClickBut2(self, e):#click Find button
#put dialog here to get find text
finddlg = wx.TextEntryDialog(self, ‘Enter text to find’, ‘Text to find’)
if finddlg.ShowModal() == wx.ID_OK:
gotit = finddlg.GetValue()
panel.tc2.SetValue(gotit)#no good
#self.statusbar.SetStatusText("gotit is = " + gotit)
finddlg.Destroy()

The example you have given is just an incomplete collection of fragments with no formatting. It also contains non-ascii single and double quotes which the Python interpreter will reject.

It would be easier to help if you could post more complete code that would at least start up. The code should be included as either preformatted text or as an attached uploaded file.

In the method OnClickBut2() you are trying to access the panel.tc2 variable which is not in scope. Even if it was in scope, you can’t address child controls using dot notation.

The InitUI() method in your example code doesn’t create a tc2 TextCtrl. Did you mean to access tc1 in OnClickBut2() ?

If you created it as an instance attribute i.e. self.tc1 in InitUI() you would be able to access it directly in OnClickBut2().

Hello Richard,
Thanks for your response and the help.
Sorry for the mess I posted, I am uploading a full version of the program as it is. I think you may have solved my problem in your last statement above. I have been thinking about the self. that I have seem in different places but don’t understand. I will certainly rewrite the code using self in front of all the widgets and see how that goes. Of course, anything else you have to say will be appreciated.

I am not seeing how to upload a file. Never mind I found it.

wxin046.py (4.4 KB)

Hello Bob,

Thank you for posting your code. It helps. I run it. The proximate cause of your trouble is that line 89 is trying to access variable panel which does not exist when that code is executed.

I believe your issue is not a wxpython specific problem but rather a conceptual object-oriented-programming in python issue. It may help to understand the basics about classes and objects: 9. Classes — Python 3.9.6 documentation

For your concrete problem, the usual approach is to make your editable widgets (e.g. the TextCtrl tc2) into attributes of the wx.Frame derived class (i.e. wxinfo) so they can be accessed in member methods (e.g. OnClickBut2).

In short, make line 61 tc2 = ... into self.tc2 = ... and access it in line 89 as self.tc2.SetValue ... (you will also need to access it in line 62 as self.tc2).

Regards

Hi Bob, thanks for uploading the code, it certainly makes it easier for others to see exactly what is happening.

As @jmoraleda has said, you really need to understand how Python implements Object Oriented programming to be able code wxPython successfully.

With reference to ‘self’ - this represents the current instance of a class. When you assign an object as an attribute of self (e.g. self.tc2) then you can access that object from other methods of the class.

Thank you both. That should solve these problems until I make some more (ha ha). I will certainly study the classes link from Jorge before I continue my project. This is a huge step forward in my progress on the project.
boB