0

I made the following GUI in DelphiFMX for Python that contains two buttons and a rectangle. I simply want to hide and show the rectangle with the button clicks:

Python GUI with two buttons and a rectangle

What I've tried doing so far is:

def ShowButton_OnClick(self, sender):
    self.myRectangle.Show()

def HideButton_OnClick(self, sender):
    self.myRectangle.Hide()

But this gives an error: Hide Button Error

What is the correct way to hide and show components?


For extra info, here's my full code:

from delphifmx import *

class frmMain(Form):
    def __init__(self, owner):
        self.Caption = 'My Form with Hide/Show Buttons'
        self.Width = 600
        self.Height = 500

        self.ShowButton = Button(self)
        self.ShowButton.Parent = self
        self.ShowButton.Width = 200
        self.ShowButton.Height = 100
        self.ShowButton.Position.X = 50
        self.ShowButton.Position.Y = 50
        self.ShowButton.Text = 'Show'
        self.ShowButton.OnClick = self.ShowButton_OnClick

        self.HideButton = Button(self)
        self.HideButton.Parent = self
        self.HideButton.Width = 200
        self.HideButton.Height = 100
        self.HideButton.Position.X = self.ShowButton.Position.X + self.ShowButton.Width + 50
        self.HideButton.Position.Y = 50
        self.HideButton.Text = 'Hide'
        self.HideButton.OnClick = self.HideButton_OnClick

        self.myRectangle = Rectangle(self)
        self.myRectangle.Parent = self
        self.myRectangle.Width = self.ShowButton.Position.X + (self.ShowButton.Width * 2)
        self.myRectangle.Height = 100
        self.myRectangle.Position.X = 50
        self.myRectangle.Position.Y = self.ShowButton.Position.Y + self.ShowButton.Height + 50

    def ShowButton_OnClick(self, sender):
        self.myRectangle.Show()

    def HideButton_OnClick(self, sender):
        self.myRectangle.Hide()


def main():
    Application.Initialize()
    Application.Title = "My Application"
    Application.MainForm = frmMain(Application)
    Application.MainForm.Show()
    Application.Run()
    Application.MainForm.Destroy()

main()

1 Answer 1

0

The correct way is to use the Visible property:

def ShowButton_OnClick(self, sender):
    self.myRectangle.Visible = True

def HideButton_OnClick(self, sender):
    self.myRectangle.Visible = False

Rectangle Hidden with "Hide" button click:

Hide and Show Python GUI Form

Not the answer you're looking for? Browse other questions tagged or ask your own question.