使用wx.TE_MULTILINE时,wxpython wx.TextCtr没有调整大小

时间:2013-06-23 18:24:52

标签: python wxpython wx.textctrl

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

  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'

  3. 继承我的代码:

    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()
    

1 个答案:

答案 0 :(得分: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()