0

hey folks using Python i have bind the radio button and when that's clicked the TextCtrl is called upon but after I type in TextCtrl i am not able to get the string that has been entered, My code goes like this

def A(self,event):
    radiobut = wx.RadioButton(self.nameofframe, label = 'Opt-1', pos = (10,70),size= (90,-1))
    self.Bind(wx.EVT_RADIOBUTTON,self.B,radiobut)
def B(self,event):
    Str1 = wx.TextCtrl(self.nameofframe,pos = (100,70), size=(180,-1))
    print Str1.GetValue()

Could anyone please tell me where is the problem . Why can't i get it printed ?

G.Jan
  • 132
  • 2
  • 10
  • It's not clear what you are trying to do here, do you want the value of `TextCtrl` is printed when the radio button is clicked? –  Nov 21 '13 at 11:31
  • @BSH Yes i want that value to be print whatever has been entered in the TextCtrl – G.Jan Nov 21 '13 at 12:40
  • @BSH The .GetValue() method is not getting the string entered in TextCtrl and thus i want to print in order to get confirmed lateron would be assigning it to somewhere else. – G.Jan Nov 21 '13 at 12:43
  • @BSH when the radio button is clicked then the TextCtrl event is generated which lets the user to enter something but after something is typed the .getmethod is not working ... I hope i am clear now – G.Jan Nov 21 '13 at 12:47

3 Answers3

2

Str1.GetValue() will be empty because when the radio button is clicked you are creating a new TextCtrl and then immediatly getting its value, it will be empty as the user has not been able to type anything in it yet.

Yoriz
  • 3,595
  • 2
  • 13
  • 20
  • Correct. Handle the EVT_TEXT_ENTER event for the text control. – ravenspoint Nov 21 '13 at 13:12
  • @Yoriz Agree! any solution for the proceeding ? i have tried somethign leme show – G.Jan Nov 21 '13 at 13:12
  • To start with you need to rethink about what are are trying to do, with the method you have now, everytime a user clicks the radio button a new TextCtrl will be made on top of the previous one. – Yoriz Nov 21 '13 at 13:23
  • Sorry Yoriz i am new to python so pardon me for that :) i am trying to handle it with EVT_TEXT_ENTER but – G.Jan Nov 21 '13 at 13:28
  • in Def B i am handling it like as follow : `self.Bind(wx.EVT_TEXT_ENTER, self.Method,Str1)` – G.Jan Nov 21 '13 at 13:29
  • but it will again go into another method – G.Jan Nov 21 '13 at 13:30
1

Here is the usual way of doing it.

Create the text control when you create the frame. Save a pointer ( sorry C++ - whatever you do with python ) to the text control and bind a method to the EVT_TEXT_ENTER event. When the event fires, you can read what the user typed.

If you want to control when and when not the text control is visible, use the hide() method.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
1

Radio button usually comes within a group, one or more more than one, and one at least should clicked but you have only one button. What is usually used in such case is a check box, CheckBox.

In this example, it prints the text entered in TextCtrl when a CheckBox is activated:

#!python
# -*- coding: utf-8 -*-

import wx

class MyFrame(wx.Frame):
  def __init__(self, title):
    super(MyFrame, self).__init__(None, title=title)

    panel = wx.Panel(self)
    self.check = wx.CheckBox(panel, label='confiurm?', pos =(10,70), size=(90,-1))
    self.text  = wx.TextCtrl(panel, pos=(100,70), size=(180,-1))
    # disable the button until the user enters something
    self.check.Disable()

    self.Bind(wx.EVT_CHECKBOX, self.OnCheck, self.check)
    self.Bind(wx.EVT_TEXT, self.OnTypeText, self.text)

    self.Centre()

  def OnTypeText(self, event):
    '''
    OnTypeText is called when the user types some string and
    activate the check box if there is a string.
    '''
    if( len(self.text.GetValue()) > 0 ):
      self.check.Enable()
    else:
      self.check.Disable()

  def OnCheck(self, event):
    '''
    Print the user input if he clicks the checkbox.
    '''
    if( self.check.IsChecked() ):
      print(self.text.GetValue())

class MyApp(wx.App):
  def OnInit(self):
    self.frame = MyFrame('Example')
    self.frame.Show()
    return True

MyApp(False).MainLoop()

This is how it works:

Step 1 Step 2 Step 3