使用下面的代码,我有两个面板,它们通过菜单进行切换。我有两个问题:
-
行
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
。我上传了一张我在这里看到的照片:http://i44.tinypic.com/2wceia1.png -
我使用
self.CreateStatusBar()
在面板底部的staus栏中传递信息,例如使用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()
在wxPython 2.9的Windows 7上,我没有看到文本控件的大小问题。你使用的是什么操作系统?至于你的另一个问题,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()