Search code examples
pythonubuntuwxpythonpackage

WxPython Text Hide Overlap Issue


I am a Python beginner trying to write an app to test presence of important security packages like IDS, Firewall, etc. There are 4 category buttons on the GUI. I want only the text to be displayed pertaining to the category. For example: If "Malicious Code Protection" is pressed, only "Firewall", "Intrusion Detection System", and "Rootkit Scanner" need to be displayed i.e. all the remaining text must be hidden. I have come up with this boilerplate code. I see that the text overlaps at times or the buttons do not hide. How can I simplify this code and ensure that the categorical display works perfectly.

   __author__ = 'kannan'
# coding: utf-8
#!/usr/bin/python

import wx, os, subprocess

class MyDialog(wx.Dialog):

def __init__(self, parent, id, title):
    wx.Dialog.__init__(self, None, wx.ID_ANY, title, pos=(0, 0), size=wx.DisplaySize())

    for i in range(113, 500):
        wx.StaticText(self, -1, '|', (480,i))

    for p in range(113, 500):
        wx.StaticText(self, -1, '|', (100,p))

    for q in range(113, 500):
        wx.StaticText(self, -1, '|', (950, q))

    for r in range(113, 500):
        wx.StaticText(self, -1, '|', (100, r))

    for s in range(113, 500):
        wx.StaticText(self, -1, '|', (950, s))


    for j in range(895, 294):
        wx.StaticText(self, -1, '_', (j,150))

    for k in range(100, 480):
        wx.StaticText(self, -1, '_', (k,200))

    for l in range(100, 480):
        wx.StaticText(self, -1, '_', (l,300))

    for m in range(100, 480):
        wx.StaticText(self, -1, '_', (m,400))

    for n in range(100, 950):
        wx.StaticText(self, -1, '_', (n,100))

    for o in range(100, 950):
        wx.StaticText(self, -1, '_', (o, 500))


    text = wx.StaticText(self, -1, "SecureIT: An Operating System Security Tool ™ ", (250,20))
    font = wx.Font(20, wx.Unconstrained, wx.ITALIC, wx.BOLD)
    text.SetFont(font)

    self.Button1 = wx.Button(self, -1, 'Malicious Code Protection', (230,140))
    self.Button1.Bind(wx.EVT_BUTTON,self.button1hide)
    self.Button1.SetBackgroundColour("yellow")
    self.Button1.SetForegroundColour("blue")

    self.Button2 = wx.Button(self, -1, 'Authentication', (230,240))
    self.Button2.Bind(wx.EVT_BUTTON, self.button2hide)
    self.Button2.SetBackgroundColour("yellow")
    self.Button2.SetForegroundColour("blue")


    self.Button3 = wx.Button(self, -1, 'Data Security',  (230,340))
    self.Button3.Bind(wx.EVT_BUTTON, self.button3hide)
    self.Button3.SetBackgroundColour("yellow")
    self.Button3.SetForegroundColour("blue")

    self.Button4 = wx.Button(self, -1, 'Miscellaneous', (230, 440 ))
    self.Button4.Bind(wx.EVT_BUTTON, self.button4hide)
    self.Button4.SetBackgroundColour("yellow")
    self.Button4.SetForegroundColour("blue")


def button1hide(self, event):

        self.Button1.Disable()
        self.Button2.Disable()
        self.Button3.Disable()
        self.Button4.Disable()


        self.btn1 = wx.StaticText(self, -1, 'Firewall ', (550, 180))
        self.btn1.SetForegroundColour("blue")

        if os.path.exists('/usr/bin/iptables-xml'):
            self.btn1.SetForegroundColour("dark green")
        else:
            self.btn1.SetForegroundColour("red")

        self.btn2 = wx.StaticText(self, -1, 'Intrusion Detection System ', (550, 260))
        self.btn2.SetForegroundColour("blue")


        if os.path.exists('/usr/bin/snort-mysql') or os.path.exists('usr/bin/acidbase'):
            self.btn2.SetForegroundColour("green")
        else:
            self.btn2.SetForegroundColour("red")
            self.btn2.SetToolTip(wx.ToolTip("Intrustion Detection"
                                            " System helps detect intruders. Kindly press the install button "
                                            "to get an IDS for your system."
                                            ""))
            self.compute_btn = wx.Button(self, 3, 'install', (750, 250))
            self.Bind(wx.EVT_BUTTON, self.IDS, id=3)

        self.btn3 = wx.StaticText(self, -1, 'Rootkit Scanner ', (550, 340))
        self.btn3.SetForegroundColour("blue")
        if os.path.exists('/usr/bin/rkhunter'):
            self.btn3.SetForegroundColour("green")
        else:
            self.btn3.SetForegroundColour("red")
            self.btn3.SetToolTip(wx.ToolTip("Rootkit Scanner not found"
                                            " System helps detect rootkits. Kindly press the install button "
                                            "to get an IDS for your system."
                                            ""))
            self.compute_btn2 = wx.Button(self, 4, 'install', (750, 330))
            self.Bind(wx.EVT_BUTTON, self.Rootkit, id=4)


        self.Button2.Enable()
        self.Button3.Enable()
        self.Button4.Enable()
        self.btn4.Hide()
        self.btn5.Hide()
        self.btn6.Hide()
        self.btn7.Hide()
        self.btn8.Hide()
        self.btn9.Hide()
        self.btn10.Hide()
        self.btn11.Hide()
        self.btn12.Hide()
        self.compute_btn4.Hide()
        self.compute_btn3.Hide()

def button2hide(self, event):

        self.Button1.Disable()
        self.Button2.Disable()
        self.Button3.Disable()
        self.Button4.Disable()


        self.btn4 = wx.StaticText(self, -1, 'Screen Lock ', (550, 180))
        self.btn4.SetForegroundColour("blue")
        self.btn5 = wx.StaticText(self, -1, 'Anonymous ', (550, 260))
        self.btn5.SetForegroundColour("blue")
        self.btn6 = wx.StaticText(self, -1, 'Guest Login ', (550, 340))
        self.btn6.SetForegroundColour("blue")

        self.Button1.Enable()
        self.Button3.Enable()
        self.Button4.Enable()


        self.btn1.Hide()
        self.btn2.Hide()
        self.btn3.Hide()
        self.btn7.Hide()
        self.btn8.Hide()
        self.btn9.Hide()
        self.btn10.Hide()
        self.btn11.Hide()
        self.btn12.Hide()
        self.compute_btn2.Hide()
        self.compute_btn.Hide()
        self.compute_btn4.Hide()
        self.compute_btn3.Hide()


def button3hide(self, event):

        self.Button1.Disable()
        self.Button2.Disable()
        self.Button3.Disable()
        self.Button4.Disable()

        self.btn7 = wx.StaticText(self, -1, 'Secure Data Deletion ', (550, 180))
        self.btn7.SetForegroundColour("blue")

        if os.path.exists('/usr/bin/wipe'):
            self.btn7.SetForegroundColour("green")
        else:
            self.bt7.SetForegroundColour("red")
            self.btn7.SetToolTip(wx.ToolTip("Rootkit Scanner not found"
                                            " System helps detect rootkits. Kindly press the install button "
                                            "to get an IDS for your system."
                                            ""))
            self.compute_btn3 = wx.Button(self,5, 'install', (750, 170))
            self.Bind(wx.EVT_BUTTON, self.wipe, id=5)

        self.btn8 = wx.StaticText(self, -1, 'Folder Encryption ', (550, 260))
        self.btn8.SetForegroundColour("blue")
        if os.path.exists('/usr/bin/encfs'):
            self.btn8.SetForegroundColour("dark green")
        else:
            self.btn8.SetForegroundColour("red")
            self.btn8.SetToolTip(wx.ToolTip("Click install to get a rootkit hunter"))
            self.compute_btn4 = wx.Button(self, 6, 'install', (750, 260))
            self.Bind(wx.EVT_BUTTON, self.encfs, id=6)


        self.btn9 = wx.StaticText(self, -1, 'ASLR ', (550, 340))
        self.btn9.SetForegroundColour("blue")

        self.Button1.Enable()
        self.Button2.Enable()
        self.Button4.Enable()
        self.btn1.Hide()
        self.btn2.Hide()
        self.btn3.Hide()
        self.btn4.Hide()
        self.btn5.Hide()
        self.btn6.Hide()
        self.btn10.Hide()
        self.btn11.Hide()
        self.btn12.Hide()
        self.compute_btn2.Hide()
        self.compute_btn.Hide()

def button4hide(self, event):

        self.Button1.Disable()
        self.Button2.Disable()
        self.Button3.Disable()
        self.Button4.Disable()

        self.btn10 = wx.StaticText(self, -1, 'Telnet ', (550, 180))
        self.btn10.SetForegroundColour("blue")
        self.btn11 = wx.StaticText(self, -1, 'Folder Encryption ', (550, 260))
        self.btn11.SetForegroundColour("blue")
        self.btn12 = wx.StaticText(self, -1, 'ASLR ', (550, 340))
        self.btn12.SetForegroundColour("blue")

        self.Button1.Enable()
        self.Button2.Enable()
        self.Button3.Enable()
        self.btn1.Hide()
        self.btn2.Hide()
        self.btn3.Hide()
        self.btn4.Hide()
        self.btn5.Hide()
        self.btn6.Hide()
        self.btn7.Hide()
        self.btn8.Hide()
        self.btn9.Hide()
        self.compute_btn4.Hide()
        self.compute_btn3.Hide()
        self.compute_btn2.Hide()
        self.compute_btn.Hide()

def Rootkit(self, event):
    os.system("gksudo \"apt-get -y install rkhunter\"")

def IDS(self, event):
    os.system("gksudo \"apt-get -y install acidbase\"")

def wipe(self, event):
    os.system("gksudo \"apt-get -y install wipe\"")

def encfs(self, event):
     os.system("gksudo \"apt-get -y install encfs\"")


class MyApp(wx.App):
def OnInit(self):
    dlg = MyDialog(None, -1, '')
    dlg.Show(True)
    dlg.Centre()
    return True

app = MyApp(0)
app.MainLoop()

Solution

  • The reason why your code not working:

    There are a lot of run time error, such as typo of "self.bt7" and btn object used before constructed. You can check these errors in the console.

    With these error, some of the code used to hide the button not called.

    Some other advice:

    1. You used hundreds of StaticText of "_" to draw the lines. It made you application huge slow. Actually you can draw line with "wx.DrawLine"

    2. When you diaplay the information by category, you need only 3 StaticText for description and 2 buttons for "install". You don't need to define so many variables(like btn10, btn11)

    I have made some changes based on you code, you can have a try:

        import wx, os, subprocess
    
        class MyDialog(wx.Dialog):
    
            def __init__(self, parent, id, title):
                wx.Dialog.__init__(self, None, wx.ID_ANY, title, pos=(0, 0), size=(1000,600))
                self.btn1 = None
                self.btn2 = None
                self.btn3 = None
                self.compute_btn1 = None
                self.compute_btn2 = None
    
            def drawAllLines(self):
                dc = wx.ClientDC(self)
                dc.SetBrush(wx.Brush('BLACK', wx.SOLID))
    
                dc.DrawLine(100, 100, 950, 100)
                dc.DrawLine(100, 200, 480, 200)
                dc.DrawLine(100, 300, 480, 300)
                dc.DrawLine(100, 400, 480, 400)
                dc.DrawLine(100, 500, 950, 500)
                dc.DrawLine(100, 100, 100, 500)
    
    
            def buildLayout(self):
                text = wx.StaticText(self, -1, "SecureIT: An Operating System Security Tool", (250,20))
                font = wx.Font(20, wx.Unconstrained, wx.ITALIC, wx.BOLD)
                text.SetFont(font)
    
                self.Button1 = wx.Button(self, -1, 'Malicious Code Protection', (230,140))
                self.Button1.Bind(wx.EVT_BUTTON,self.button1hide)
                self.Button1.SetBackgroundColour("yellow")
                self.Button1.SetForegroundColour("blue")
    
                self.Button2 = wx.Button(self, -1, 'Authentication', (230,240))
                self.Button2.Bind(wx.EVT_BUTTON, self.button2hide)
                self.Button2.SetBackgroundColour("yellow")
                self.Button2.SetForegroundColour("blue")
    
    
                self.Button3 = wx.Button(self, -1, 'Data Security',  (230,340))
                self.Button3.Bind(wx.EVT_BUTTON, self.button3hide)
                self.Button3.SetBackgroundColour("yellow")
                self.Button3.SetForegroundColour("blue")
    
                self.Button4 = wx.Button(self, -1, 'Miscellaneous', (230, 440 ))
                self.Button4.Bind(wx.EVT_BUTTON, self.button4hide)
                self.Button4.SetBackgroundColour("yellow")
                self.Button4.SetForegroundColour("blue")
    
                self.drawAllLines()
    
            def clearAllBtn(self):
                if self.btn1:
                    self.btn1.Hide()
                if self.btn2:
                    self.btn2.Hide()
                if self.btn3:
                    self.btn3.Hide()
                if self.compute_btn2:
                    self.compute_btn2.Hide()
                if self.compute_btn1:
                    self.compute_btn1.Hide()
    
            def button1hide(self, event):
                self.clearAllBtn()
    
                self.btn1 = wx.StaticText(self, -1, 'Firewall ', (550, 180))
                self.btn1.SetForegroundColour("blue")
    
                if os.path.exists('/usr/bin/iptables-xml'):
                    self.btn1.SetForegroundColour("dark green")
                else:
                    self.btn1.SetForegroundColour("red")
    
                self.btn2 = wx.StaticText(self, -1, 'Intrusion Detection System ', (550, 260))
                self.btn2.SetForegroundColour("blue")
    
    
                if os.path.exists('/usr/bin/snort-mysql') or os.path.exists('usr/bin/acidbase'):
                    self.btn2.SetForegroundColour("green")
                else:
                    self.btn2.SetForegroundColour("red")
                    self.btn2.SetToolTip(wx.ToolTip("Intrustion Detection"
                                                    " System helps detect intruders. Kindly press the install button "
                                                    "to get an IDS for your system."
                                                    ""))
                    self.compute_btn1 = wx.Button(self, 3, 'install', (750, 250))
                    self.Bind(wx.EVT_BUTTON, self.IDS, id=3)
    
                self.btn3 = wx.StaticText(self, -1, 'Rootkit Scanner ', (550, 340))
                self.btn3.SetForegroundColour("blue")
                if os.path.exists('/usr/bin/rkhunter'):
                    self.btn3.SetForegroundColour("green")
                else:
                    self.btn3.SetForegroundColour("red")
                    self.btn3.SetToolTip(wx.ToolTip("Rootkit Scanner not found"
                                                    " System helps detect rootkits. Kindly press the install button "
                                                    "to get an IDS for your system."
                                                    ""))
                    self.compute_btn2 = wx.Button(self, 4, 'install', (750, 330))
                    self.Bind(wx.EVT_BUTTON, self.Rootkit, id=4)
    
    
            def button2hide(self, event):
                self.clearAllBtn()
    
                self.btn1 = wx.StaticText(self, -1, 'Screen Lock ', (550, 180))
                self.btn1.SetForegroundColour("blue")
                self.btn2 = wx.StaticText(self, -1, 'Anonymous ', (550, 260))
                self.btn2.SetForegroundColour("blue")
                self.btn3 = wx.StaticText(self, -1, 'Guest Login ', (550, 340))
                self.btn3.SetForegroundColour("blue")
    
    
            def button3hide(self, event):
                self.clearAllBtn()
    
                self.btn1 = wx.StaticText(self, -1, 'Secure Data Deletion ', (550, 180))
                self.btn1.SetForegroundColour("blue")
    
                if os.path.exists('/usr/bin/wipe'):
                    self.btn1.SetForegroundColour("green")
                else:
                    self.btn1.SetForegroundColour("red")
                    self.btn1.SetToolTip(wx.ToolTip("Rootkit Scanner not found"
                                                    " System helps detect rootkits. Kindly press the install button "
                                                    "to get an IDS for your system."
                                                    ""))
                    self.compute_btn1 = wx.Button(self,5, 'install', (750, 170))
                    self.Bind(wx.EVT_BUTTON, self.wipe, id=5)
    
                self.btn2 = wx.StaticText(self, -1, 'Folder Encryption ', (550, 260))
                self.btn2.SetForegroundColour("blue")
                if os.path.exists('/usr/bin/encfs'):
                    self.btn2.SetForegroundColour("dark green")
                else:
                    self.btn2.SetForegroundColour("red")
                    self.btn2.SetToolTip(wx.ToolTip("Click install to get a rootkit hunter"))
                    self.compute_btn2 = wx.Button(self, 6, 'install', (750, 260))
                    self.Bind(wx.EVT_BUTTON, self.encfs, id=6)
    
                self.btn3 = wx.StaticText(self, -1, 'ASLR ', (550, 340))
                self.btn3.SetForegroundColour("blue")
    
    
            def button4hide(self, event):
                self.clearAllBtn()
    
                self.btn1 = wx.StaticText(self, -1, 'Telnet ', (550, 180))
                self.btn1.SetForegroundColour("blue")
                self.btn2 = wx.StaticText(self, -1, 'Folder Encryption ', (550, 260))
                self.btn2.SetForegroundColour("blue")
                self.btn3 = wx.StaticText(self, -1, 'ASLR ', (550, 340))
                self.btn3.SetForegroundColour("blue")
    
    
            def Rootkit(self, event):
                os.system("gksudo \"apt-get -y install rkhunter\"")
    
            def IDS(self, event):
                os.system("gksudo \"apt-get -y install acidbase\"")
    
            def wipe(self, event):
                os.system("gksudo \"apt-get -y install wipe\"")
    
            def encfs(self, event):
                os.system("gksudo \"apt-get -y install encfs\"")
    
    
        class MyApp(wx.App):
            def OnInit(self):
                dlg = MyDialog(self, -1, '')
                dlg.Show(True)
                dlg.Centre()
                dlg.buildLayout()
                return True
    
        app = MyApp(0)
        app.MainLoop()