【问题标题】:Wxpython: How to access variables between across classes/panelsWxpython:如何跨类/面板访问变量
【发布时间】:2015-05-16 05:32:13
【问题描述】:

我是 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()

【问题讨论】:

  • 这里有很多不可读的代码。问一个问题,修正你的格式,只发布必要的代码,以及你的预期和实际输出或行为。
  • @Scott 我已经编辑了程序,它现在可以执行了。如果你能帮助我了解如何在类 GUI 中获得的输入(即 IP 地址、用户名、pswd)可以是在类 panelone 和 paneltwo 中访问..
  • 我还需要一个建议,如果我使用菜单栏切换面板的方法是对还是错..我有点担心,因为我将有大约 14-15 个不同的面板部分...如果有人也可以指导这将非常有帮助

标签: python user-interface wxpython


【解决方案1】:

您可以使用例如访问面板类中的框架属性

parent.username

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

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

"wx.InitAllImageHandlers" 应该不再需要了,除非你使用一个非常旧的 wxPython 版本。

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

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    相关资源
    最近更新 更多