0

Working on a user interface and some of the text inputs are prefilled with default information. AS seen in the below screencap the text that I have preset is both highlighted and hidden behind the left edge of the TextCtrl. How can I fix this?

Screen Cap:

The text is highlighted when the window-frame is in focus on Windows. It wasn't when I snipped it. The fulle text is "/var/lib/cmt/tomcat/CDES"

Image

(Sorry for image size, I don't know how to resize it in SOF's post interface)

Directly relavent code:

    DirPath_O_Text = wx.StaticText(panel,label='Remote Directory Path', style=wx.ALIGN_RIGHT)
    self.DirPath_O_TC = wx.TextCtrl(panel)
    self.DirPath_O_TC.ChangeValue("/var/lib/cmt/tomcat/CDES")

    hbox1.Add(DirPath_O_Text, proportion=1,flag=wx.EXPAND|wx.BOTTOM,border=10)
    hbox1.Add((25,-1))
    hbox1.Add(self.DirPath_O_TC, proportion=1,)
    hbox1.Add((25,-1))

Full Code:

import wx

class Popup(wx.Frame):

    def __init__(self, *args, **kwargs): #initializing
        super(Popup, self).__init__(*args, **kwargs)

        self.SetMinSize((500,400)) #change this to change the minimum size of the total frame
        self.Centre()
        self.InitUI()
        self.Show() 


    def InitUI(self): #building the UI

        panel = wx.Panel(self) #main panel that holds everything

        #setting font
        font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(16) #this is the size for the title, its smaller later on

        #setting panel colour
        # background_Colour = '#BCD7E0'
        # panel.SetBackgroundColour(background_Colour)

        ###########################     
        #vbox = sizer holds the whole thing
        #  Title = label
        #  hbox1 = sizer
        #     Remote Directory Label
        #     Remote Directory TextCtrl
        #  hbox2 = sizer
        #     Local Directory Label
        #     Local Directory TextCtrl
        #  hbox3 = sizer
        #     Hostname Label
        #     Hostname TextCtrl
        #  hbox4 = sizer
        #     Username Label
        #     Username TextCtrl
        #  hbox5 = sizer
        #     Password Label
        #     Password TextCtrl
        #  vbox2 = just used to align the hbox6 to center
        #     hbox6 = sizer that holds the bottom two buttons
        #        ok button = sends all info to next program
        #        Cancel button = closes current program

        ###############
        #start
        vbox = wx.BoxSizer(wx.VERTICAL) #oriented verticaly

        #########
        #title      
        st_title = wx.StaticText(panel,label='CMT-CDES XML Messaging Tool', style=wx.ALIGN_CENTRE)
        st_title.SetFont(font)
        font.SetPointSize(9) #changes font back to small for all other text besides title

        vbox.Add(st_title, proportion=1,flag=wx.EXPAND|wx.ALL, border=10) #adds the title
        ######

        ########
        #6 sizers. 1-5 are for the inputs, 6 is for buttons on bottom
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        hbox4 = wx.BoxSizer(wx.HORIZONTAL)
        hbox5 = wx.BoxSizer(wx.HORIZONTAL)
        hbox6 = wx.BoxSizer(wx.HORIZONTAL)
        ########


        ########
        #set labels and TC for each hbox

        #hbox1
        DirPath_O_Text = wx.StaticText(panel,label='Remote Directory Path', style=wx.ALIGN_RIGHT)
        self.DirPath_O_TC = wx.TextCtrl(panel)
        self.DirPath_O_TC.ChangeValue("/var/lib/cmt/tomcat/CDES")

        hbox1.Add(DirPath_O_Text, proportion=1,flag=wx.EXPAND|wx.BOTTOM,border=10)
        hbox1.Add((25,-1))
        hbox1.Add(self.DirPath_O_TC, proportion=1,)
        hbox1.Add((25,-1))

        #hbox2
        DirPath_L_Text = wx.StaticText(panel,label='Local Directory Path', style=wx.ALIGN_RIGHT)
        self.DirPath_L_TC = wx.TextCtrl(panel)

        hbox2.Add(DirPath_L_Text, proportion=1,flag=wx.EXPAND|wx.BOTTOM,border=10)
        hbox2.Add((25,-1))
        hbox2.Add(self.DirPath_L_TC, proportion=1, )
        hbox2.Add((25,-1))

        #hbox3
        Host_Text = wx.StaticText(panel,label='Hostname\IP', style=wx.ALIGN_RIGHT)
        self.Host_TC = wx.TextCtrl(panel)

        hbox3.Add(Host_Text, proportion=1,flag=wx.EXPAND|wx.BOTTOM,border=10)
        hbox3.Add((25,-1))
        hbox3.Add(self.Host_TC, proportion=1)
        hbox3.Add((25,-1))

        #hbox4
        User_Text = wx.StaticText(panel,label='Username', style=wx.ALIGN_RIGHT)
        self.User_TC = wx.TextCtrl(panel)

        hbox4.Add(User_Text, proportion=1,flag=wx.EXPAND|wx.BOTTOM,border=10)
        hbox4.Add((25,-1))
        hbox4.Add(self.User_TC, proportion=1)
        hbox4.Add((25,-1))

        #hbox5
        Pass_Text = wx.StaticText(panel,label='Password', style=wx.ALIGN_RIGHT)
        self.Pass_TC = wx.TextCtrl(panel, style=wx.TE_PASSWORD)

        hbox5.Add(Pass_Text, proportion=1,flag=wx.EXPAND|wx.BOTTOM,border=10)
        hbox5.Add((25,-1))
        hbox5.Add(self.Pass_TC, proportion=1)
        hbox5.Add((25,-1))
        ########

        ########
        #buttons
        btn_size = (90, 30)
        btn_OK = wx.Button(panel, label='OK', size=btn_size)
        btn_Cancel = wx.Button(panel, label='Cancel', size=btn_size)

        #bindings
        btn_OK.Bind(wx.EVT_BUTTON, self.OK) #binds to ok (just takes variables from all text boxes, and passes to program)
        btn_Cancel.Bind(wx.EVT_BUTTON, self.Cancel) #binds to cancel (just closes the window)

        #holds buttons
        hbox6.Add(btn_OK)
        hbox6.Add((25,-1)) #spacer between buttons
        hbox6.Add(btn_Cancel, flag=wx.ALIGN_RIGHT,proportion=0)

        #aligns hbox
        vbox2 = wx.BoxSizer(wx.VERTICAL) #added this vbox to set the alignment to center
        vbox2.Add(hbox6, flag=wx.ALIGN_CENTRE, proportion = 0)
        #######

        ############
        #add everything to main vbox
        vbox.Add(hbox1,proportion=1,flag=wx.BOTTOM|wx.EXPAND, border=10)
        vbox.Add(hbox2,proportion=1,flag=wx.BOTTOM|wx.EXPAND, border=10)
        vbox.Add(hbox3,proportion=1,flag=wx.BOTTOM|wx.EXPAND, border=10)
        vbox.Add(hbox4,proportion=1,flag=wx.BOTTOM|wx.EXPAND, border=10)
        vbox.Add(hbox5,proportion=1,flag=wx.BOTTOM|wx.EXPAND, border=10)

        #buttons vbox
        vbox.Add(vbox2,proportion=1,flag=wx.BOTTOM|wx.EXPAND, border=10)
        ############

        #add main vbox to panel
        panel.SetSizer(vbox)

    def OK(self, e):
        #creates a 5 item list, [DirPath_O, DirPath_L, Host, User, Pass]
        args = [str(self.DirPath_O_TC.GetValue()) , str(self.DirPath_L_TC.GetValue()),str(self.Host_TC.GetValue()), str(self.User_TC.GetValue()), str(self.Pass_TC.GetValue())]

        #print "args[1:5]: ", args

        '''TODO
        '''
        #call main, pass it args
        #self.Close() #closes window when done
        '''
        '''

        #TODO: Test if values are correct

        print #not needed, just extra

    def Cancel(self, e): #closes window
        self.Close()



#this is the main loop
ex = wx.App()
Popup(None)
ex.MainLoop()
Alex M
  • 67
  • 5

1 Answers1

0

I believe that the text is left aligned by default. If you'd like it right-aligned, I believe you can use the text control's SetWindowStyle method along with the wx.TE_RIGHT flag to do so.

As for why the first text control highlights the text, the reason for that is that the first text control is also the first widget which can accept focus. Because it gets the focus and it contains text, the text gets highlighted automatically.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Left Aligned is fine, what I worry about is the fact that half the text is cut off on the left. Its as if the program thinks the start of TextCtrl is somewhere left of where it is displayed. – Alex M Sep 09 '16 at 14:07
  • Try calling `SetInsertionPoint(0)` – Mike Driscoll Sep 09 '16 at 15:26
  • I tried calling that on the TextCtrl and it made no difference unfortunately. – Alex M Sep 09 '16 at 15:33
  • 3
    Use `wx.CallAfter(theTextCtrl.SetInsertionPoint, 0)`, because it needs to happen after the text has been selected, which is when the focus is set there. – RobinDunn Sep 09 '16 at 16:34