2

I have a simply code:

import wx

class Glowne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        pos = 55
        tekst = 'HELLO - position'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        wx.StaticText(self, -1, tekst, (300, pos)).SetFont(font)

        btn = wx.Button(self, -1, "Change pos", (345, 100))
        #self.Bind(wx.EVT_BUTTON, Program.zmiana, btn)

class Program(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,'Program')
        panel_one = Glowne(self)
        self.SetSize((800,600))
        self.Centre()

if __name__ == "__main__":
    app = wx.App(False)
    frame = Program()
    frame.Show()
    app.MainLoop()

How can I modyfy pos variable after hitting "Change pos" button? In my real program I have something like: name name1 name2

"BUTTTON"

I would like to add wx.TextCtrl method between name2 and button after hitting "BUTTON". I need to modyfy a frame (add place beetwen name2 and button). I do not know how can I achieve that.

EDIT. There is a code I need to modify:

def __init__(self, parent):
    global odstep
    self.panel = wx.Panel.__init__(self, parent)
    odstep = 0
    odstep1 = 0
    font = wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
    for name in config['rewir1']:
        nazwa_zliczana = config['rewir1'][name]
        odstep += 22
        self.name1 = wx.StaticText(self, -1, name, (300, 10 + odstep))
        self.name1.SetFont(font)
        btn_usuwanie = wx.Button(self, -1, u"Usuń", (475, 10 + odstep))
        self.Bind(wx.EVT_BUTTON, lambda evt, i: Program.Usuwanie(evt, i), btn_usuwanie)
    wx.StaticText(self, -1, 'PART I', (365, 0), style=wx.ALIGN_CENTER).SetFont(font)
    odstep1 = odstep + 50
    print odstep
    for name in config['rewir2']:
        nazwa_zliczana = config['rewir2'][name]
        odstep1 += 22
        self.name2 = wx.StaticText(self, -1, name, (300, 50 + odstep1))
        self.name2.SetFont(font)
        btn_usuwanie_2 = wx.Button(self, -1, u"Usuń", (475, 50 + odstep1))
        self.Bind(wx.EVT_BUTTON, lambda evt, i: Program.Usuwanie(evt, i), btn_usuwanie_2)
    print odstep1
    wx.StaticText(self, -1, 'PART II', (365, 80 + odstep), style=wx.ALIGN_CENTER).SetFont(font)
    self.btn = wx.Button(self, -1, "Change panel", (345, 500))
    self.btn_dodaj_rewir1 = wx.Button(self, -1, "Add name", (345, 42 + odstep))
    self.btn_dodaj_rewir2 = wx.Button(self, -1, "Add name", (345, 84 + odstep1))
    self.Bind(wx.EVT_BUTTON, self.new_name, self.btn_dodaj_rewir1)

1 Answer 1

1
  • You need to save wx.StaticText instanced. To change property of it later.
    • Use SetPosition method to change position.
  • Bind EVT_BUTTON event to event handler (change_pos in the following code).

class Glowne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        tekst = 'HELLO - position'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        self.text = wx.StaticText(self, -1, tekst, (300, 55))  # Save for later use
        self.text.SetFont(font)

        btn = wx.Button(self, -1, "Change pos", (345, 100))
        btn.Bind(wx.EVT_BUTTON, self.change_pos)

    def change_pos(self, event):
        x, y = self.text.Position
        self.text.SetPosition((x, y + 10))
Sign up to request clarification or add additional context in comments.

4 Comments

I have another problem. In my program there is a for loop. I need to modify positions of all items. When I use SetPosition it takes only last item from loop. Any ideas?
@Luk, Could you post another new question with code that demonstrate your issue?
I modified my question and added some code. Should I start new topic?
@Luk, "new" question please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.