2013-06-23 11 views
0

使用下面的代碼我有兩個面板,它們通過菜單進行切換。 我有兩個問題:wxpython wx.TextCtr使用wx.TE_MULTILINE時不會調整大小

  1. self.txtfilein1 = wx.TextCtrl(self, -1, pos=(35, 112), size=(300, 165), style = wx.TE_MULTILINE)似乎並不注意size=(300, 165),它比填充面板,同時採用wx.TE_MULTILINE。如果我拿出wx.TE_MULTILINE那麼它的大小正確,麻煩是我需要wx.TE_MULTILINE。我已經上傳awhat的形象,我在這裏看到:http://i44.tinypic.com/2wceia1.png

  2. 我使用self.CreateStatusBar()通過在我的面板底部的居留制條信息,例如通過使用self.SetStatusText("Your selected directory is: %s" % pathoutdir),但我得到的錯誤AttributeError: 'PanelOne' object has no attribute 'SetStatusText'

繼承人我的代碼:

import wx 
import os 
import os.path 
import inspect 
import subprocess 
import sys 

class PanelOne(wx.Panel): 
    """""" 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent=parent) 

     distros = ['None', 'Option 1', 'Option 2', 'Option 3', 'Option 4'] 
     cb = wx.ComboBox(self, pos=(35, 40), choices=distros, 
      style=wx.CB_READONLY) 
     cb.Bind(wx.EVT_COMBOBOX, self.OnSelect) 

     self.txtfilein1 = wx.TextCtrl(self, -1, pos=(35, 112), size=(300, 165), style = wx.TE_MULTILINE) 

     self.pathoutdir = wx.TextCtrl(self, -1, pos=(35, 320), size=(300, 25)) 
     self.buttonout = wx.Button(self, -1, "Open", pos=(350,318)) 
     self.buttonout.Bind(wx.EVT_BUTTON, self.openoutdir) 

     self.buttonGo = wx.Button(self, -1, "Go", pos=(120,370)) 
     self.buttonGo.Bind(wx.EVT_BUTTON, self.go) 

     self.buttonClose = wx.Button(self, -1, "Quit", pos=(235,370)) 
     self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose) 

     provider = '''Provider''' 
     inputtxt = '''Enter text''' 
     outputtxt = '''Output Directory''' 
     wx.StaticText(self, -1, provider, (33, 22), style=wx.ALIGN_CENTRE) 
     wx.StaticText(self, -1, inputtxt, (33, 90), style=wx.ALIGN_CENTRE) 
     wx.StaticText(self, -1, outputtxt, (33, 300), style=wx.ALIGN_CENTRE) 

    def go(self, edit): 
     global txtin 
     txtin = (self.txtfilein1.GetValue()) 
     self.runLookup(self) 

    def openoutdir(self, event): 
     dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON) 
     if dlg.ShowModal() == wx.ID_OK: 
      global pathoutdir 
      pathoutdir = dlg.GetPath() 
      self.SetStatusText("Your selected directory is: %s" % pathoutdir) 
     self.pathoutdir.Clear() 
     self.pathoutdir.write(pathoutdir) 
     dlg.Destroy() 

    def OnSelect(self, e): 
     global provider 
     provider = e.GetString() 

    def OnClose(self, e): 
     self.Close(True) 

########################################################## 

class PanelTwo(wx.Panel): 
    """""" 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 

     distros = ['None', 'Option 1', 'Option 2', 'Option 3', 'Option 4'] 

     cb = wx.ComboBox(self, pos=(35, 40), choices=distros, 
      style=wx.CB_READONLY) 
     cb.Bind(wx.EVT_COMBOBOX, self.OnSelect) 

     self.txtfilein = wx.TextCtrl(self, -1, pos=(35, 114), size=(300, 25)) 
     self.buttonfilein = wx.Button(self, -1, " Open File ", pos=(350,111)) 
     self.buttonfilein.Bind(wx.EVT_BUTTON, self.openfile) 

     self.pathindir = wx.TextCtrl(self, -1, pos=(35, 174), size=(300, 25)) 
     self.buttonin = wx.Button(self, -1, "Open Directory", pos=(350,170)) 
     self.buttonin.Bind(wx.EVT_BUTTON, self.openindir) 

    def openfile(self, event): 
     dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN) 
     if dlg.ShowModal() == wx.ID_OK: 
      global txtfilein 
      txtfilein = dlg.GetPath() 
      self.SetStatusText("Your selected file is: %s" % txtfilein) 
     self.txtfilein.Clear() 
     self.txtfilein.write(txtfilein) 
     dlg.Destroy() 

    def openindir(self, event): 
     dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE|wx.DD_DIR_MUST_EXIST) 
     if dlg.ShowModal() == wx.ID_OK: 
      global pathindir 
      pathindir = dlg.GetPath() 
      self.SetStatusText("Your selected directory is: %s" % pathindir) 
     self.pathindir.Clear() 
     self.pathindir.write(pathindir) 
     print pathindir 
     dlg.Destroy() 

    def OnSelect(self, e): 
     global provider 
     provider = e.GetString() 

################################################ 

class MyForm(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, 
          "Panel 1", size=(550, 650)) 

     self.panel_one = PanelOne(self)    
     self.panel_two = PanelTwo(self) 
     self.panel_two.Hide() 

     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.panel_one, 1, wx.EXPAND) 
     self.sizer.Add(self.panel_two, 1, wx.EXPAND) 
     self.SetSizer(self.sizer) 

     self.CreateStatusBar() 
     menubar = wx.MenuBar() 
     fileMenu = wx.Menu() 

     fileMenu.Append(99, "&Panel 1", "Panel 1") 
     fileMenu.Append(100, "&Panel 2", "Panel 2") 
     self.Bind(wx.EVT_MENU, self.onSwitchPanels1, id=99) 
     self.Bind(wx.EVT_MENU, self.onSwitchPanels2, id=100) 

     menubar.Append(fileMenu, '&File') 
     self.SetMenuBar(menubar) 

    def onSwitchPanels1(self, event): 
     """""" 
     self.SetTitle("Panel 1") 
     self.panel_one.Show() 
     self.panel_two.Hide() 
     self.Layout() 

    def onSwitchPanels2(self, event): 
     """""" 
     self.SetTitle("Panel 2") 
     self.panel_one.Hide() 
     self.panel_two.Show() 
     self.Layout() 

if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 

回答

0

我沒有看到在Windows 7中的文本控件的wxPython 2.9上漿問題。你在什麼操作系統上?至於你的其他問題,wx正確響應。該面板沒有這種方法。您需要保存對狀態欄的引用,然後以此方式進行更新。這裏有一個稍微修改版本的代碼,顯示了一種方法:

import wx 
import os 
import os.path 
import inspect 
import subprocess 
import sys 

class PanelOne(wx.Panel): 
    """""" 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent=parent) 

     distros = ['None', 'Option 1', 'Option 2', 'Option 3', 'Option 4'] 
     cb = wx.ComboBox(self, pos=(35, 40), choices=distros, 
      style=wx.CB_READONLY) 
     cb.Bind(wx.EVT_COMBOBOX, self.OnSelect) 

     self.txtfilein1 = wx.TextCtrl(self, -1, pos=(35, 112), size=(300, 165), style = wx.TE_MULTILINE) 

     self.pathoutdir = wx.TextCtrl(self, -1, pos=(35, 320), size=(300, 25)) 
     self.buttonout = wx.Button(self, -1, "Open", pos=(350,318)) 
     self.buttonout.Bind(wx.EVT_BUTTON, self.openoutdir) 

     self.buttonGo = wx.Button(self, -1, "Go", pos=(120,370)) 
     self.buttonGo.Bind(wx.EVT_BUTTON, self.go) 

     self.buttonClose = wx.Button(self, -1, "Quit", pos=(235,370)) 
     self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose) 

     provider = '''Provider''' 
     inputtxt = '''Enter text''' 
     outputtxt = '''Output Directory''' 
     wx.StaticText(self, -1, provider, (33, 22), style=wx.ALIGN_CENTRE) 
     wx.StaticText(self, -1, inputtxt, (33, 90), style=wx.ALIGN_CENTRE) 
     wx.StaticText(self, -1, outputtxt, (33, 300), style=wx.ALIGN_CENTRE) 

    def go(self, edit): 
     global txtin 
     txtin = (self.txtfilein1.GetValue()) 
     self.runLookup(self) 

    def openoutdir(self, event): 
     dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON) 
     if dlg.ShowModal() == wx.ID_OK: 
      global pathoutdir 
      pathoutdir = dlg.GetPath() 
      self.SetStatusText("Your selected directory is: %s" % pathoutdir) 
     self.pathoutdir.Clear() 
     self.pathoutdir.write(pathoutdir) 
     dlg.Destroy() 

    def OnSelect(self, e): 
     global provider 
     provider = e.GetString() 

    def OnClose(self, e): 
     self.Close(True) 

########################################################## 

class PanelTwo(wx.Panel): 
    """""" 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 
     self.frame = parent 

     distros = ['None', 'Option 1', 'Option 2', 'Option 3', 'Option 4'] 

     cb = wx.ComboBox(self, pos=(35, 40), choices=distros, 
      style=wx.CB_READONLY) 
     cb.Bind(wx.EVT_COMBOBOX, self.OnSelect) 

     self.txtfilein = wx.TextCtrl(self, -1, pos=(35, 114), size=(300, 25)) 
     self.buttonfilein = wx.Button(self, -1, " Open File ", pos=(350,111)) 
     self.buttonfilein.Bind(wx.EVT_BUTTON, self.openfile) 

     self.pathindir = wx.TextCtrl(self, -1, pos=(35, 174), size=(300, 25)) 
     self.buttonin = wx.Button(self, -1, "Open Directory", pos=(350,170)) 
     self.buttonin.Bind(wx.EVT_BUTTON, self.openindir) 

    def openfile(self, event): 
     dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN) 
     if dlg.ShowModal() == wx.ID_OK: 
      global txtfilein 
      txtfilein = dlg.GetPath() 
      self.SetStatusText("Your selected file is: %s" % txtfilein) 
     self.txtfilein.Clear() 
     self.txtfilein.write(txtfilein) 
     dlg.Destroy() 

    def openindir(self, event): 
     dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE|wx.DD_DIR_MUST_EXIST) 
     if dlg.ShowModal() == wx.ID_OK: 
      global pathindir 
      pathindir = dlg.GetPath() 
      self.frame.myStatusBar.SetStatusText("Your selected directory is: %s" % pathindir) 
     self.pathindir.Clear() 
     self.pathindir.write(pathindir) 
     print pathindir 
     dlg.Destroy() 

    def OnSelect(self, e): 
     global provider 
     provider = e.GetString() 

################################################ 

class MyForm(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, 
          "Panel 1", size=(550, 650)) 

     self.panel_one = PanelOne(self) 
     self.panel_two = PanelTwo(self) 
     self.panel_two.Hide() 

     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.panel_one, 1, wx.EXPAND) 
     self.sizer.Add(self.panel_two, 1, wx.EXPAND) 
     self.SetSizer(self.sizer) 

     self.myStatusBar = self.CreateStatusBar() 
     menubar = wx.MenuBar() 
     fileMenu = wx.Menu() 

     fileMenu.Append(99, "&Panel 1", "Panel 1") 
     fileMenu.Append(100, "&Panel 2", "Panel 2") 
     self.Bind(wx.EVT_MENU, self.onSwitchPanels1, id=99) 
     self.Bind(wx.EVT_MENU, self.onSwitchPanels2, id=100) 

     menubar.Append(fileMenu, '&File') 
     self.SetMenuBar(menubar) 

    def onSwitchPanels1(self, event): 
     """""" 
     self.SetTitle("Panel 1") 
     self.panel_one.Show() 
     self.panel_two.Hide() 
     self.Layout() 

    def onSwitchPanels2(self, event): 
     """""" 
     self.SetTitle("Panel 2") 
     self.panel_one.Hide() 
     self.panel_two.Show() 
     self.Layout() 

if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 
+0

嗨,我在OSX 10.8.4上,標準的python 2.7.2。 – speedyrazor

+0

您是否將文本控件放在您的末端的sizer中?這肯定會導致它擴大。雖然我沒有看到你的例子。 –

+0

使用您編輯的示例,這是我所看到的:http://i44.tinypic.com/2wceia1.png – speedyrazor

相關問題