简体   繁体   English

Python 2.6:如何导入我编写和使用的类

[英]Python 2.6: How to import a class that I wrote and use it

I'm new to Python programming. 我是Python编程的新手。 I have 6-8 small desktop applications I want to write that will use a GUI. 我要编写使用GUI的6-8个小型桌面应用程序。 I thought to be smart, I would build my basic GUI shell and then import it into each of my apps. 我以为很聪明,我会构建基本的GUI Shell,然后将其导入到每个应用程序中。 That way they will all have the same look and feel. 这样,它们将具有相同的外观。 Also, as I enhance the base GUI package, those changes will flow to all my little apps. 另外,当我增强基本GUI程序包时,这些更改将流到我所有的小应用程序中。

I'm having trouble figuring out how to properly import and then reference the GUI. 我在弄清楚如何正确导入然后引用GUI时遇到了麻烦。 Below is my base GUI package first. 下面是我的基本GUI软件包。 After that, I show what I've done to try and import it. 在那之后,我展示了尝试导入它的工作。 It actually imports but I can't change any of the variables. 它实际上是导入的,但是我不能更改任何变量。 I'm completely lost. 我完全迷路了。

If someone could point me in the right direction without getting too technical I would really appreciate it. 如果有人能在不掌握太多技术的情况下为我指明正确的方向,我将非常感激。 I'm still confused on the whole OOP thing. 我仍然对整个OOP感到困惑。

Base GUI package 基本GUI包


# bsgui which stands for Base GUI is the basic GUI for starting new apps
#
# The variables are set up so you can edit them easily for your new 
# Application

# Versioning
var_Version = "1.00"
var_Title = "bsgui   Version: %s" % var_Version
var_HelpAboutMbxTitle = "About bsgui"
var_Menu_Custom = "XXXX"
print ("Hello World")

import wx

# Create the Main window

class cls_gui(wx.Frame):

    def __init__(self,parent,id):
    wx.Frame.__init__(self,parent,id, var_Title)

# Add the panel to the frame
    pnl_gui_main=wx.Panel(self)

    # Menu Bar and structure
    wig_menu_bar=wx.MenuBar()

     # Create the File Menu Tree
    mnu_File=wx.Menu()
     wig_menu_bar.Append(mnu_File,"&File")
     mnu_Edit=wx.Menu()
     wig_menu_bar.Append(mnu_Edit, "&Edit")
     mnu_Options=wx.Menu()
    wig_menu_bar.Append(mnu_Options, "&Options")
    mnu_Custom=wx.Menu()
    wig_menu_bar.Append(mnu_Custom, var_Menu_Custom)
    mnu_Tools=wx.Menu()
    wig_menu_bar.Append(mnu_Tools, "&Tools")
    mnu_Help=wx.Menu()
    wig_menu_bar.Append(mnu_Help, "&Help")

     # These are options under the File menubar option
    printMI = mnu_File.Append(wx.NewId(), "&Print", "Print the current record")
    exitMI = mnu_File.Append(wx.NewId(), "E&xit", "Exit the program")
    self.Bind(wx.EVT_MENU, self.fcn_closebutton, exitMI)

    # Help tree options
    helpMI = mnu_Help.Append(wx.NewId(), "&About", "About the program")
    self.Bind(wx.EVT_MENU, self.fcn_HelpAbout, helpMI)


    # This statment makes the menubar appear 
    self.SetMenuBar(wig_menu_bar)

    # Create the status bar in the bottom of the app
    wig_status_bar=self.CreateStatusBar()

    # Add a button to the panel
    btn_exit=wx.Button(pnl_gui_main,label="Exit",pos=(5,575),
                            size=(50,30))
    self.Bind(wx.EVT_BUTTON, self.fcn_closebutton, btn_exit)

    # Make the X button close when you click it
    self.Bind(wx.EVT_CLOSE, self.fcn_closewindow)

# This function closes the app when the button is clicked
def fcn_closebutton(self,event):
    self.Close(True)

 # This function displays the Help / About information
def fcn_HelpAbout(self, event):
    mbx_HelpAbout=wx.MessageDialog(None, var_Title, 
                    var_HelpAboutMbxTitle,wx.OK)
    mbx_HelpAbout_Answer=mbx_HelpAbout.ShowModal()
    mbx_HelpAbout.Destroy()

# This function closes the app when the X is clicked
def fcn_closewindow(self, event):
    self.Destroy()

if __name__ == "__main__":
    app = wx.PySimpleApp() #application object - runs your program
    frame=cls_gui(parent=None,id=-1) #frame object - need a frame
    frame.Show()
    frame.Maximize()
    app.MainLoop() # kicks off the program

Here is how I got bsgui to import and open but can't access any of the variables 这是我如何导入和打开bsgui但无法访问任何变量的方法


import wx
import bsgui

# use the following 2 lines to see what path Python is using then any 
# you want to import needs to be put in one of the path folders. After 
# that you can use the import command. Python files should not include 
# the .py extension

# import sys
# print sys.path


if __name__ == "__main__":
    app = wx.PySimpleApp() #application object - runs your program
    frame=bsgui.cls_gui(parent=None,id=-1) #frame object - need a frame
    frame.Show()
    frame.Maximize()
    app.MainLoop() # kicks off the program

That's because you created local variables. 那是因为您创建了局部变量。 They stop existing when the function/method returns. 当函数/方法返回时,它们停止存在。 If you want to make them persistent then you need to create them as attributes on the object. 如果要使它们持久化,则需要在对象上将它们创建为属性。

self.pnl_gui_main=wx.Panel(self)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM