无法切换菜单的可见性

iugsix8n  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(339)

win-10、jdk/jre 1.8、netbeans 8.2、jython 2.7、python 3.9
我是一个程序员爱好者,我的代码基础来自在线示例(谢谢)。我在80年代做过一些程序编程,但我很难跳转到面向对象编程。我认为我的根本问题是不理解如何找到要操纵的对象;尤其是在课间。
我运行desktopframe来生成我的gui,从菜单中我所有的“\u intro”操作都可以工作。方法“menu pick”找到我执行的所有其他操作并将它们传递给类(切换器)切换器工作中的所有方法,我可以从类(locationpanel)中的方法“list\u all\u jmri\u locations”打开一个jframe(书籍和/或在线示例建议将受到热烈欢迎)

我的目标

我正在尝试从菜单“mode”中筛选菜单栏,并切换菜单“locations”的可见性,以便当(mode=stand-alone)菜单栏显示“menu2a”,当(mode=jmri)菜单栏显示“menu2”

我的问题

方法菜单\u pick提供以下错误消息:
文件“c:\users\jwkel\documents\netbeansprojects\jythonproject\src\desktop.py”,第40行,在menu pick if self.menu2.isselected()中:attributeerror:'desktopframe'对象没有属性'menu2'
我似乎找不出正确的方法来指向“menu”或“menu2”。我试过使用“self”、“demo”、“desktopframe”和简单的“menu2”。我试过把它当作jython属性和直接赋值。我试图找到一个类方法来使用,但他们也失败了。
如果我将测试移到类(切换器)方法“stand\u alone”,我会收到错误消息,指出“menu2 not declared”

from javax.swing import *
from myTest import *
from javax import imageio
from java import io
from locationIntro import *
import sys
from switcher import *

class DesktopFrame(JFrame):
    """
    Base GUI code     By: Jim Kelly 12-6-20
    """

    def __init__(self):
        """
        Creates the Main program frame
        """
        JFrame.__init__(self,
                        'Serial RFID Layout Monitor',
                        size=(1200, 700),
                        defaultCloseOperation=JFrame.EXIT_ON_CLOSE)

        self.setLayout(BorderLayout())
        self.setBackground(Color.LIGHT_GRAY)
        self.create_menus()
        self.image("rfid_pic.jpg")
        self.text = '''Getting Started:\n\n1 - Connect the Hardware\n\n
        2 - Assign COMM port\n\n
        3 - Tell the program how many readers you have\n\n
        4 - Assign JMRI locations to readers\n''' 
        self.main_pane(self.text)

    def menu_pick(self, event):  
        '''Capture which menu item was selected'''
        c=event.getActionCommand()
        #print c
        if c == "Stand Alone":
            print "stand alone",c
            if self.menu2.isSelected():
                print "is selected"
            else:
                print "not selected"
        elif c =="JMRI":
            print "JMRI",c
            self.menubar.menu2a.visible=0
            self.menubar.menu2.visible=1
        #else:
        d = c.replace(" ","_")
        e = d.lower()
        a=Switcher()
        a.get_selection(e)

    def image(self, fileName):
        '''Put picture on the Introduction panels'''
        self.panel = JPanel()
        self.panel.setLayout(BorderLayout())
        self.panel.setBackground(Color.LIGHT_GRAY)
        _image_filename = fileName
        self.showImage = imageio.ImageIO.read(io.File(_image_filename))
        self.panel.add(JLabel(ImageIcon(self.showImage)))
        self.add(self.panel, BorderLayout.WEST)
        self.pack()

    def main_pane(self, text):
        ''' Put introduction text into mainPanel'''
        _text = text
        self.mainText = JTextArea()
        self.mainText.text = _text
        self.mainText.lineWrap = True
        self.mainText.wrapStyleWord = True
        self.mainText.setSize(400, 250)
        self.mainText.setBackground(Color.LIGHT_GRAY)
        self.mainScroll = JScrollPane(self.mainText)
        self.mainScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS)
        self.mainScroll.setPreferredSize(Dimension(400, 250))
        self.mainScroll.getViewport().setView((self.mainText))
        self.mainPanel = JPanel()
        self.mainPanel.setBackground(Color.LIGHT_GRAY)
        self.mainPanel.add(self.mainScroll)
        self.add(self.mainPanel, BorderLayout.EAST)
        self.pack()

    def rfid_intro(self, event):  
        '''Basic setup instruction on Main frame'''
        self.mainPanel.visible = False
        self.panel.visible = False
        self.text = '''Getting Started:\n\n1 -******TEXT CLIPPED******")

    def locations_intro(self, event):  
        '''About text explains what locations are.'''
        self.mainPanel.visible = False
        self.panel.visible = False
        self.text = ("Serial RFID Locations:\n\n "
            "NOTE: This menu is filtered******TEXT CLIPPED******.")
        self.main_pane(self.text)
        self.image("locations_pic.jpg")  

    def engines_intro(self, event):  
        '''About text explains what engines are.'''
        self.mainPanel.visible = False
        self.panel.visible = False
        self.text = ("Serial RFID Engines:\n\n" 
            "Serial RFID uses the term******TEXT CLIPPED******")
        self.main_pane(self.text)
        self.image("engine_pic.jpg")

    def readers_intro(self, event):  
        '''About text explains what readers are.'''
        self.mainPanel.visible = False
        self.panel.visible = False
        self.text = ("Serial RFID Readers:\n\n"
            "Readers are the location assignments.******TEXT CLIPPED******") 

        self.main_pane(self.text)
        self.image("readers_pic.jpg")

    def cars_intro(self, event):  
        '''About text explains what cars are.'''
        self.mainPanel.visible = False
        self.panel.visible = False
        self.text = ("Serial RFID Cars:\n\n"
        "NOTE: This menu is filtered acording to the mode you have seleccted.\n\n"
        "JMRI Mode:\n"******TEXT CLIPPED******" ) 

        self.main_pane(self.text)
        self.image("cars_pic.jpg")

    def mode_intro(self, event):  
        '''About text explains what modes are available.'''
        self.mainPanel.visible = False
        self.panel.visible = False
        self.text = ("'Serial RFID Modes:\n\n "
        "Serial RFID has 2 modes of operation.\n\n******TEXT CLIPPED******")
        self.main_pane(self.text)
        self.image("mode_pic.jpg")

    def create_menus(self):
        '''Main frame menu items'''
        menu = JMenu('File')
        menu.add(JMenuItem('About Serial RFID', actionPerformed=self.rfid_intro))
        menu.add(JMenuItem('Exit', actionPerformed=self.menu_pick))
        menu1 = JMenu('Mode')
        menu1.add(JMenuItem('About modes', actionPerformed=self.mode_intro))
        alone = JRadioButtonMenuItem('Stand Alone', actionPerformed=self.menu_pick, selected=1)
        jmri = JRadioButtonMenuItem('JMRI', actionPerformed=self.menu_pick)
        bGroup = ButtonGroup()
        bGroup.add(alone)
        bGroup.add(jmri)
        menu1.add(alone)
        menu1.add(jmri)

        menu2 = JMenu('Locations',visible=0)
        menu2.add(JMenuItem('About RFID Locations', actionPerformed=self.locations_intro))
        menu2.add(JMenuItem('List all JMRI Locations', actionPerformed=self.menu_pick,visible=0))
        menu2.add(JMenuItem('Link JMRI Location to a Reader', actionPerformed=self.menu_pick,visible=0))
        menu2.add(JMenuItem('Compare a JMRI Location List to a Reader', actionPerformed=self.menu_pick,visible=0))

        menu2a = JMenu('A Locations',visible=1)
        menu2.add(JMenuItem('About RFID Locations', actionPerformed=self.locations_intro))
        menu2.add(JMenuItem('List Cars from a Reader', actionPerformed=self.menu_pick,visible=1))
        menu2.add( JMenuItem('Add Text Location to a Reader', actionPerformed=self.menu_pick,visible=1))

        menu3 = JMenu('Engines')
        menu3.add(JMenuItem('About RFID Engines', actionPerformed=self.engines_intro))
        menu3.add(JMenuItem('Create Engine', actionPerformed=self.menu_pick))
        menu3.add(JMenuItem('Asign to location', actionPerformed=self.menu_pick))
        menu3.add(JMenuItem('Pick up Car', actionPerformed=self.menu_pick))
        menu3.add(JMenuItem('Drop off Car', actionPerformed=self.menu_pick))
        menu3.add(JMenuItem('List Cars in Engine', actionPerformed=self.menu_pick))
        menu3.add(JMenuItem('Terminate Engine', actionPerformed=self.menu_pick))
        menu3.add(JMenuItem('Delete Engine', actionPerformed=self.menu_pick))
        menu4 = JMenu('Readers')
        menu4.add(JMenuItem('About RFID Readers', actionPerformed=self.readers_intro))
        menu4.add(JMenuItem('Readers', actionPerformed=self.menu_pick))
        menu4.add(JMenuItem('Add reader', actionPerformed=self.menu_pick))
        menu4.add(JMenuItem('Delete reader', actionPerformed=self.menu_pick))
        menu5 = JMenu('Cars')
        menu5.add(JMenuItem('About RFID Cars', actionPerformed=self.cars_intro))
        menu5.add(JMenuItem('Read Car from JMRI list', actionPerformed=self.menu_pick,visible=0))
        menu5.add(JMenuItem('Assign RFID tag to Car', actionPerformed=self.menu_pick,visible=0))
        menu5.add(JMenuItem('Manual Mode Car Programming', actionPerformed=self.menu_pick,visible=1))

        menubar = JMenuBar()
        menubar.add(menu)
        menubar.add(menu1)
        menubar.add(menu2)
        menubar.add(menu2a)
        menubar.add(menu3)
        menubar.add(menu4)
        menubar.add(menu5)

        self.setJMenuBar(menubar)

if __name__ == '__main__':
    demo = DesktopFrame()
    demo.setLocation(30, 30)
    demo.show()
from locationIntro import *
import sys

class Switcher(object):
    '''Match menu selection to a function'''
    def get_selection(self, argument):
        """Convert menu text to a function"""
        method_name = str(argument)
        # Get the method from 'self'. Default to a lambda.
        method = getattr(self, method_name, lambda: "Invalid Pick")
        # Call the method as we return it
        return method()

    def exit(self): 
        '''end program'''
        sys.exit()

    def stand_alone(self): 
        '''end program'''
        print 'made 2a'

    def jmri(self): 
        '''end program'''
        print "made 2"

    def list_all_jmri_locations(self):
        ''' run JMRI XML file and get layout locations.'''
        m = LocationPanel()
        m.show()
        return "January"

    def link_jmri_location_to_a_reader(self):
        '''asign location(s) to a reader'''
        return "February"

类剪辑****

soat7uwm

soat7uwm1#

我转到菜单定义(menubar、menu、menuitem)并在每个条目中添加“self”,然后在method menu pick中添加代码

def menu_pick(self, event):  
        '''Capture which menu item was selected'''
        c=event.getActionCommand()
        if c == "Stand Alone":
            print "pick stand alone"
            self.menu2.visible=0 
            self.menu2a.visible=1

        if c =="JMRI":
            print "pick JMRI"
            self.menu2.visible=1 
            self.menu2a.visible=0

        d = c.replace(" ","_")
        e = d.lower()
        a=Switcher()
        a.get_selection(e)

现在我的菜单可见切换工作。

相关问题