0

使用下面的代码,我有两个面板,它们通过菜单切换。我有两个问题:

  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. 我已经上传了我在这里看到的图片: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()
4

1 回答 1

0

我没有看到带有 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()
于 2013-06-24T14:32:37.457 回答