我是一名Python初学者,试图编写一个应用来测试IDS,防火墙等重要安全软件包的存在.GUI上有4个类别按钮。我只希望显示与该类别相关的文本。例如:如果按下“恶意代码保护”,则只需显示“防火墙”,“入侵检测系统”和“Rootkit扫描程序”,即必须隐藏所有剩余文本。我想出了这个样板代码。我看到文本有时重叠或按钮不隐藏。如何简化此代码并确保分类显示完美运行。
__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()
答案 0 :(得分:1)
您的代码无效的原因:
有很多运行时错误,例如“self.bt7”的错误和构造之前使用的btn对象。您可以在控制台中检查这些错误。
出现这些错误,一些代码用于隐藏未调用的按钮。
其他一些建议:
您使用了数百个“_”的StaticText来绘制线条。它让你的应用程序变得非常慢。实际上你可以用“wx.DrawLine”
当您按类别显示信息时,您只需要3个StaticText用于描述,2个按钮用于“安装”。您不需要定义这么多变量(例如btn10,btn11)
我根据您的代码做了一些更改,您可以尝试一下:
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()