簡體   English   中英

Python 2.6:如何導入我編寫和使用的類

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

我是Python編程的新手。 我要編寫使用GUI的6-8個小型桌面應用程序。 我以為很聰明,我會構建基本的GUI Shell,然后將其導入到每個應用程序中。 這樣,它們將具有相同的外觀。 另外,當我增強基本GUI程序包時,這些更改將流到我所有的小應用程序中。

我在弄清楚如何正確導入然后引用GUI時遇到了麻煩。 下面是我的基本GUI軟件包。 在那之后,我展示了嘗試導入它的工作。 它實際上是導入的,但是我不能更改任何變量。 我完全迷路了。

如果有人能在不掌握太多技術的情況下為我指明正確的方向,我將非常感激。 我仍然對整個OOP感到困惑。

基本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

這是我如何導入和打開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

那是因為您創建了局部變量。 當函數/方法返回時,它們停止存在。 如果要使它們持久化,則需要在對象上將它們創建為屬性。

self.pnl_gui_main=wx.Panel(self)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM