2012-09-19 36 views
1

我正嘗試使用wxWizard類創建一個簡單的安裝程序。在第一頁(頁0)我想有3個選項wxPython - 創建嚮導,可根據用戶選擇更改「下一頁」頁面

1. Install (click Next goes to page install1) 
2. Upgrade (click Next goes to page upgrade1) 
3. Remove (click Next goes to page remove1) 

由於我缺乏與OOP經驗(和一般的編程),我無法理解如何創建PAGE0對象會做這個。

如果我創建頁0/0 install1前:全局名稱「install1」沒有定義 如果我PAGE0之前創建install1:最大遞歸深度超過 如果我把班SkipNextPage在http://xoomer.virgilio.it/infinity77/wxPython/wizard/wx.wizard.html:有一個時髦的GetNext()方法也別不明白。

請幫忙!

import wx 
import wx.wizard 

class InstallPage_Dyn(wx.wizard.PyWizardPage): 
    def __init__(self, parent, title): 
     wx.wizard.PyWizardPage.__init__(self, parent) 
     self.next = self.prev = None 


class InstallPage0(wx.wizard.PyWizardPage): 
    def __init__(self, parent, title): 
     wx.wizard.PyWizardPage.__init__(self, parent) 
     self.next = self.prev = None 
     self.box = wx.RadioBox (self, -1, 'Choose one of the options below and hit Next\n', choices=['Install','Upgrade','Remove'], style = wx.VERTICAL | wx.EXPAND) 

     # Set Next button based on user choice 
     self.box.Bind(wx.EVT_RADIOBOX, self.SetNext(install1)) 

    # Setter and getter methods to specify Next and Previous buttons#  
    def SetNext(self, next): 
     userchoice = self.box.GetSelection() 
     if userchoice == 0: 
      self.SetNext(install1) 
     elif userchoice == 1: 
      self.SetNext(upgrade1) 
     elif userchoice == 2: 
      self.SetNext(remove1) 

    def SetPrev(self, prev): 
     return self.prev 

    def GetNext(self): 
     return self.next 

    def GetPrev(self): 
     return self.prev 


# Define application and create the wizard 
app = wx.App() 

wizard = wx.wizard.Wizard(None, -1, "Installer") 
wizard.SetPageSize((500,350)) 

# User selected install. Create the pages 
install1 = InstallPage_Dyn(wizard, "Install") 
upgrade1 = InstallPage_Dyn(wizard, "Upgrade") 
remove1 = InstallPage_Dyn(wizard, "Remove") 

# Create page instances 
page0 = InstallPage0(wizard, "Installer") 

wizard.RunWizard(page0) 

回答

1

試試這個

class InstallPage_Dyn(wx.wizard.PyWizardPage): 
    def __init__(self, parent, title): 
     wx.wizard.PyWizardPage.__init__(self, parent) 
     self.title = wx.StaticText(self,-1,title) 
     self.title.SetFont(wx.Font(40,wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_BOLD)) 
     self.next = self.prev = None 
    def SetPrev(self,prev): 
     self.prev = prev 
    def GetPrev(self): 
     return self.prev 

class InstallPage0(wx.wizard.PyWizardPage): 
    def __init__(self, parent, title,optional_panels = {}): 
     wx.wizard.PyWizardPage.__init__(self, parent) 
     self.prev = self 
     self.next = optional_panels.values()[0] 
     self.box = wx.RadioBox (self, -1, 'Choose one of the options below and hit Next\n', choices=optional_panels.keys(), style = wx.VERTICAL | wx.EXPAND) 
     self.opts = optional_panels.keys() 
     self.pages = optional_panels.values() 
     for p in self.pages: 
      p.SetPrev(self) 
     self.next = self.pages[0] 
     self.optional_panels = optional_panels 

    def GetNext(self): 
     return self.pages[self.box.GetSelection()] 
    def GetPrev(self): 
     return self.prev 
... 
page0 = InstallPage0(wizard, "Installer",{'install':install1,'upgrade':upgrade1,'remove':remove1}) 

wizard.RunWizard(page0) 
#app.MainLoop() 

這裏是完整的代碼......它命名爲wiz.py並運行它

import wx 
import wx.wizard 

class InstallPage_Dyn(wx.wizard.PyWizardPage): 
    def __init__(self, parent, title): 
     wx.wizard.PyWizardPage.__init__(self, parent) 
     self._title = title 
     self.title = wx.StaticText(self,-1,title) 
     self.title.SetFont(wx.Font(40,wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_BOLD)) 
     self.next = self.prev = None 
    def SetPrev(self,prev): 
     self.prev = prev 
    def GetPrev(self): 
     return self.prev 

class InstallPage0(wx.wizard.PyWizardPage): 
    def __init__(self, parent, title,optional_panels = {}): 
     wx.wizard.PyWizardPage.__init__(self, parent) 
     self.prev = self 
     self.next = optional_panels[0] 
     options = [p._title for p in optional_panels] 
     self.box = wx.RadioBox (self, -1, 'Choose one of the options below and hit Next\n', choices=options, style = wx.VERTICAL | wx.EXPAND) 
     self.pages = optional_panels 
     for p in self.pages: 
      p.SetPrev(self) 
     self.next = install1 
     self.optional_panels = optional_panels 
    def SetPrev(self, prev): 
     self.prev = prev 
     return self.prev 
    def GetNext(self): 
     return self.pages[self.box.GetSelection()] 
    def GetPrev(self): 
     return self.prev 


# Define application and create the wizard 
app = wx.App(redirect=False) 

wizard = wx.wizard.Wizard(None, -1, "Installer") 
wizard.SetPageSize((500,350)) 

# User selected install. Create the pages 
install1 = InstallPage_Dyn(wizard, "Install") 
upgrade1 = InstallPage_Dyn(wizard, "Upgrade") 
remove1 = InstallPage_Dyn(wizard, "Remove") 

# Create page instances 
page0 = InstallPage0(wizard, "Installer",[install1,upgrade1,remove1]) 

wizard.RunWizard(page0) 
+0

謝謝Joran。不幸的是,它不適合我。 Page0打開,但不是Next按鈕,它顯示'完成'按鈕。幾乎就好像page0.next從未設置。 – sce

+0

你是什麼意思?...它應該沒問題,我只是從我的文件中發佈的原始代碼,對我來說運行良好...你會得到什麼錯誤? –

+0

視頻證明:http://screencast.com/t/aG7lpSosOaE –