【发布时间】:2020-12-09 14:47:21
【问题描述】:
Win-10、JDK/JRE 1.8、NetBeans 8.2、Jython 2.7、Python 3.9
我是一名业余程序员,我的代码库来自在线示例(谢谢。)我在 80 年代做过一些程序编程,但我很难跳到 OOP。我认为我的根本问题是不了解如何找到要操作的对象;尤其是课间。
我运行 DeskTopFrame 来生成我的 GUI,从菜单中我的所有“_intro”操作都有效。方法“menu_pick”找到我所有的其他 actionPerformed 并将它们传递给 Class(Switcher) 所有 Switcher 工作的方法,我可以在方法“list_all_jmri_locations”中从 Class(LocationPanel) 打开一个 Jframe(书籍和/或在线示例推荐将受到热烈欢迎。)
我的目标
我正在尝试从菜单“模式”中过滤菜单栏并切换菜单“位置”的可见性,这样当(模式=独立)菜单栏显示“menu2a”和(模式=JMRI)菜单栏显示时“菜单2”
我的问题
方法 menu_pick 给我以下错误信息:
文件 "C:\Users\jwkel\Documents\NetBeansProjects\JythonProject\src\desktop.py", 第 40 行,在 menu_pick 中 if self.menu2.isSelected(): AttributeError: 'DesktopFrame' object has no attribute 'menu2'
我似乎无法找出指向“menu”或“menu2”的正确方法。我试过使用“self”、“demo”、“deskTopFrame”和简单的“menu2”。我尝试将其视为 Jython 属性和直接赋值。我试图找到一种可以使用的类方法,但它们也失败了。
如果我将测试移至 Class(switcher) 方法“stand_alone”,我会收到错误消息,指出“menu2 未声明”
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"
****** 类剪辑 **********
【问题讨论】: