我有一個使用PyWizardPage創建的嚮導。它從一個主頁開始,然後用戶通過多個頁面請求輸入信息。在其中一頁上,我創建了一個瀏覽按鈕,要求用戶選擇一個文件。然後,在下一頁上,我有一個「驗證詳細信息」的東西,它顯示一個框,其中包含用戶通過嚮導選擇的所有輸入,並要求他在繼續之前確保信息是正確的。如何在wxPython PyWizardPage嚮導的頁面之間傳遞信息?
我的問題是,我不能得到任何前面的頁面上的用戶輸入傳遞到我的「驗證詳細信息」頁面。
對於我的設置,我有一個WizardPage類,我打電話在我的嚮導中創建每個頁面。
mywizard.py
import wx
import wx.wizard
class WizardPage(wx.wizard.PyWizardPage):
def __init__(self, parent, title):
wx.wizard.PyWizardPage.__init__(self, parent)
self.next = None
self.prev = None
self.initializeUI(title)
def initializeUI(self, title):
# create grid layout manager
self.sizer = wx.GridBagSizer()
self.SetSizerAndFit(self.sizer)
def addWidget(self, widget, pos, span):
self.sizer.Add(widget, pos, span, wx.EXPAND)
# getters and setters
def SetPrev(self, prev):
self.prev = prev
def SetNext(self, next):
self.next = next
def GetPrev(self):
return self.prev
def GetNext(self):
return self.next
然後,在一個單獨的文件,我有一個函數來創建每個頁面。第一頁的功能調用函數來創建其他頁面。我也使用名爲「browse1」和「browse2」的幫助函數來打開wx.FileDialog
等等。
pages.py
import wx
from mywizard import *
#### HELPER FUNCTIONS ####
def browse1(event, parent_page):
wildcards = ...
dlg = wx.FileDialog(parent_page, "Blahblah", "", "",
wildcards, wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
global x1
global bro1
x1.SetValue(dlg.GetDirectory() + "\\" + dlg.GetFilename())
bro1 = x1.GetValue()
def browse2(event, parent_page):
wildcards = ...
dlg = wx.FileDialog(parent_page, "Select new certificate", "", "",
wildcards, wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
global x2
global bro2
x2.SetValue(dlg.GetDirectory() + "\\" + dlg.GetFilename())
bro2 = x2.GetValue()
#### END HELPER FUNCTIONS ####
def create_page1(wizard):
page1 = WizardPage(wizard, "Page 1")
d = wx.StaticText(...)
page1.addWidget(d, (2, 1), (1,5))
page2 = create_update_certs_page2(wizard)
page3 = create_update_certs_page3(wizard)
# Set links
page2.SetPrev(page1)
page1.SetNext(page2)
page3.SetPrev(page2)
page2.SetNext(page3)
return page1
def create_page2(wizard):
page2 = WizardPage(wizard, "Page 2")
b1 = wx.Button(page2, -1, "Browse...", wx.DefaultPosition, (85, 23))
b1.Bind(wx.EVT_BUTTON, lambda evt: browse1(evt, page2))
global x1
x1 = wx.TextCtrl(...) # the text area where the Browse path is populated
b2 = wx.Button(page2, -1, "Browse...", wx.DefaultPosition, (85, 23))
b2.Bind(wx.EVT_BUTTON, lambda evt: browse2(evt, page2))
global x2
x2 = wx.TextCtrl(...) # the text area where the Browse path is populated
page2.addWidget(b1, ..., ...)
page2.addWidget(b2, ..., ...)
page2.addWidget(x1, ..., ...)
page2.addWidget(x2, ..., ...)
return page2
def create_page3(wizard):
print "bro1 is" + bro1 # these print empty strings
print "bro2 is" + bro2 # not, the browse path and file that should be populated
基本上,x1
和x2
指的是我做了全球,所以我可以填充它們在browse1
和browse2
功能wx.TextCtrl
部件。現在,我想訪問create_page3
函數中的bro1
和bro2
變量中的填充值,但沒有顯示任何內容。
而且在pages.py的主要代碼:
app = wx.App(redirect = False)
wizard = wx.wizard.Wizard(None, -1, "Some Title")
wizard.SetPageSize((500, 350))
mypage1 = create_page1(wizard)
# Let's go!
wizard.RunWizard(mypage1)
任何幫助將不勝感激。
他們爲什麼要成爲全球?如果您需要在其他函數或頁面中使用它們的值,請考慮將它們作爲參數傳遞給您的函數或類。 – msvalkon