Wxpython:如何跨类/面板访问变量

时间:2015-05-16 05:32:14

标签: python user-interface wxpython

我是wxpython和OOPS概念的新手。 我的要求是一个窗口,如图所示。 目前的问题是如何访问用户在面板编号2类和函数中输入的帧IP地址,用户名和密码。

另外我需要一个建议,如果我使用菜单栏切换面板的方法是对还是错?我将使用 14到15 不同的panel2布局..每个支付将有不同的列表,复选框和文本框..

请指导我或提供建议..

提前致谢。

Layout of the window:Frame(Black): accepts ip add,username,password. Panle2(Orange):Will need to switch to different panel(Just a portion of the frame) or contents of panel should change depending on the user selecting the menu bar .Text Box of Frame 3(Red):Shows the output depending on the panel 2 selection and execution

请找到我正在使用的代码如下(执行):

import wx
import wx.lib.scrolledpanel


class PanelTwo(wx.Panel):
""""""

#----------------------------------------------------------------------
def __init__(self, parent):
    """Constructor"""
    screenSize = wx.DisplaySize()
    screenWidth = screenSize[0]
    screenHeight = screenSize[1]
    wx.Panel.__init__(self, parent=parent,pos=(0,175),size=(screenWidth,570),style=wx.SIMPLE_BORDER)
    radio3 = wx.RadioButton(parent=self, label="Enable", name='radio3', pos=(0,50))      
class PanelOne(wx.Panel):
""""""

#----------------------------------------------------------------------
def __init__(self, parent):
    screenSize = wx.DisplaySize()
    screenWidth = screenSize[0]
    screenHeight = screenSize[1]
    wx.Panel.__init__(self, parent=parent,pos=(0,175),size=(screenWidth,570),style=wx.SIMPLE_BORDER)
    radio3 = wx.RadioButton(parent=self, label="Disable", name='radio3', pos=(10,150))      

class GUI(wx.Frame):
def __init__(self, parent,id,title):

    # BOA generated methods
    #First retrieve the screen size of the device
    screenSize = wx.DisplaySize()
    screenWidth = screenSize[0]
    screenHeight = screenSize[1]
    #Create a frame
    wx.Frame.__init__(self,parent,id,title,size=screenSize, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
    self.SetBackgroundColour('#d8d8d8')
    self.ipt=wx.StaticText(parent=self,id=-1,label="IDRAC IP Address",pos=(10,30))
    font1 = wx.Font(12,wx.DEFAULT,wx.NORMAL,wx.NORMAL)
    self.ipt.SetFont(font1)
    self.ipaddress=wx.TextCtrl(parent=self,pos=(155,30),size=(150,25))#Question1:how can I access ipaddress obtained here in class panelone and class paneltwo?
    self.usert=wx.StaticText(parent=self,id=-1,label="User Name  ",pos=(10,65))
    font2 = wx.Font(12,wx.DEFAULT,wx.NORMAL,wx.NORMAL)
    self.usert.SetFont(font2)
    self.username=wx.TextCtrl(parent=self,pos=(155,65),size=(150,25))
    self.passt=wx.StaticText(parent=self,id=-1,label="Password  ",pos=(10,100))
    font3 = wx.Font(12,wx.DEFAULT,wx.NORMAL,wx.NORMAL)
    self.passt.SetFont(font3)
    self.password=wx.TextCtrl(parent=self,pos=(155,100),size=(150,25),style=wx.TE_PASSWORD)
    self.checkbtn=wx.Button(parent=self,label='Connect' ,pos=(320,45),size=(65,65))
    self.checkbtn.Bind(wx.EVT_BUTTON, self.connect)
    self.con=wx.StaticText(parent=self,id=-1,label="Connectivity Status",pos=(400,30))
    self.textarea=wx.TextCtrl(parent=self,style=wx.TE_READONLY|wx.TE_MULTILINE,pos=wx.Point(10,760),size=(screenWidth,270))
    self.status=wx.TextCtrl(parent=self,style=wx.TE_READONLY,pos=wx.Point(575,30),size=(170,25))
    self.con=wx.StaticText(parent=self,id=-1,label="Connectivity Status",pos=(400,30))

    font3 = wx.Font(12,wx.DEFAULT,wx.NORMAL,wx.NORMAL)
    self.con.SetFont(font3)
    self.panel_one = PanelOne(self)
    self.panel_two = PanelTwo(self)
    self.panel_two.Hide()
    menubar = wx.MenuBar()
    fileMenu = wx.Menu()
    switch_panels_menu_item = fileMenu.Append(wx.ID_ANY,
                                              "Switch Panels",
                                              "Some text")
    self.Bind(wx.EVT_MENU, self.onSwitchPanels,
              switch_panels_menu_item)
    menubar.Append(fileMenu, '&File')
    self.SetMenuBar(menubar)
 def connect(self,event):
    print "Connect"

def onSwitchPanels(self, event):

    if self.panel_one.IsShown():
       self.SetTitle("Panel Two Showing")
       self.panel_one.Hide()
       self.panel_two.Show()
    else:
       self.SetTitle("Panel One Showing")
       self.panel_one.Show()
       self.panel_two.Hide()
    self.Layout()


if __name__ == '__main__':
app = wx.App()
wx.InitAllImageHandlers()
frame = GUI(parent=None, id=-1, title="BIOS/iDRAC tokens")
frame.Show()
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

您可以使用例如

访问面板类中的框架属性
parent.username

你将如何处理15个左右的面板,有15个菜单选项,还是......?

使用隐藏/显示是一个选项,也许是具有不同面板的书籍类型小部件(例如笔记本)将是另一种选择。

除非你使用一个非常古老的wxPython版本,否则不应再需要“wx.InitAllImageHandlers”。

顺便说一下,代码的缩进级别仍然不正确。

相关问题