首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何告诉wxpython向右sizer添加文本控件

如何告诉wxpython向右sizer添加文本控件
EN

Stack Overflow用户
提问于 2015-03-16 15:35:15
回答 1查看 250关注 0票数 0

在下面的代码中,我试图动态地向sizer添加新的文本控件。我有两个测量器,一个在左边("sizer"),另一个在右边("sizer2")。有一个名为"add_region“的函数,它应该将新的文本控件添加到右侧的sizer (sizer2)中。我已经将此函数绑定到"button_add“按钮。当我从代码的主体(行:self.add_region(0) )直接调用函数self.add_region(0)时,文本控件将按需要添加到sizer2中。但是,通过点击按钮来调用相同的函数,文本控件会被放在不同的sizer (想必是"sizer")中。为什么文本控件的位置取决于函数的调用方式?

代码语言:javascript
复制
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#This is a good tutorial: http://wiki.wxpython.org/wxPython%20by%20Example

import wx
import wx.lib.scrolledpanel



class region():

    #variables associated with the entire class (not each object)
    nreg=0

    def __init__(self,Ani=999.0e29,AZ=9.99):

        self.Ani=Ani
        self.AZ=AZ
        # self.text_Ani
        region.nreg+=1



class MainWindow(wx.Frame): #A "frame" is what we normally think of as a window, so we have called it MainWindow



    def __init__(self, parent, title):    
        super(   MainWindow, self).__init__(parent, title=title, size=(450, 350)   )

        self.dirname = ''
        self.ibox=0
        self.iv=0 #the vertical depth on the LHS
        self.iw=0 #the vertical depth on the RHS
        self.Aregion_objects=[]
        self.Ani=[] #array of ion number densities
        self.AZ=[]
        self.Axmin=[]
        self.Axmax=[]
        self.Aymin=[]
        self.Aymax=[]
        self.text_Ani=[]
        self.tc_Ani=[]
        self.sizer = wx.GridBagSizer(5, 1)
        #also declare a sizer that will contain all the regions on the LHS (sizer2)
        self.sizer2 = wx.GridBagSizer(1, 1)
        #self.SetMinSize(self.GetSize()) #prevents the window from being made arbitrarily small
        self.SetClientSize( (900,900) )
        self.SetMinSize( (-1,-1) )
        self.InitGUI()
        self.Centre()
        # self.SetupScrolling()
        self.Show()     



    def InitGUI(self):



        screenSize = wx.DisplaySize()
        screenWidth = screenSize[0]
        screenHeight = screenSize[1]


        self.panel = wx.lib.scrolledpanel.ScrolledPanel(self)
        # self.panel.Layout()
        self.panel.SetupScrolling()

        font_title = wx.Font(10, wx.DECORATIVE, wx.NORMAL, wx.BOLD)

        #self.panel.SetBackgroundColour('#4f5049')

        #a sizer is a way of laying out widgets, it is not a window in itself.
        # This is why we create widgets in the self.panel (by using "self.panel" as the parent), then afterwards add the widget to the sizer.
        sizer = self.sizer #wx.GridBagSizer(5, 1)
        #also declare a sizer that will contain all the regions on the LHS (sizer2)
        sizer2 = self.sizer2 #wx.GridBagSizer(1, 1)
        # sizer = wx.BoxSizer()

        #make a horizontal box dividing the page into left and RHS. We will put the gridbagsizer into the
        #LHS and the target picture into the RHS of the hbox
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(sizer,2) #add the box to the current window (self)


        #make a vertical box that will contain the stuff on the RHS
        vbox_RHS = wx.BoxSizer(wx.VERTICAL)
        hbox.Add(vbox_RHS,2) #add the vbox into the hbox, so it now takes up the 2nd position in the hbox (the gridbagsizer took the first)

        text_view = wx.StaticText(self.panel, label="Target View")
        text_view.SetFont(font_title)
        vbox_RHS.Add(text_view, flag=wx.TOP|wx.LEFT|wx.BOTTOM,border=15)
        self.iw+=1

        button_redraw = wx.Button(self.panel, label="Redraw")
        self.Bind(wx.EVT_BUTTON, self.redraw_pic, button_redraw)
        vbox_RHS.Add(button_redraw)
        self.iw+=1

        self.pic = wx.StaticBitmap( self.panel, -1, wx.Bitmap("small1.jpg", wx.BITMAP_TYPE_ANY),size=(400,250) )
        vbox_RHS.Add(self.pic)
        self.iw+=1



        vbox_RHS.Add(sizer2)






        button_check = wx.Button(self.panel, label="Check")
        self.Bind(wx.EVT_BUTTON, self.check_all, button_check)
        sizer.Add(button_check, pos=(self.iv, 0))
        self.iv+=1



        text_PIC = wx.StaticText(self.panel, label="Main PIC Options")
        text_PIC.SetFont(font_title)
        sizer.Add(text_PIC, pos=(self.iv, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM,border=15)
        self.iv+=1





        self.tc_info = wx.TextCtrl(self.panel)
        sizer.Add(self.tc_info, pos=(self.iv, 1), span=(1, 3), flag=wx.TOP)
        self.iv+=1

        self.add_region(0)

        button_add = wx.Button(self.panel, label="Add Region")
        self.Bind(wx.EVT_BUTTON, self.add_region, button_add)
        vbox_RHS.Add(button_add)
        self.iw+=1





        sizer.AddGrowableCol(2)

        self.panel.SetSizer(hbox)



    # GUI EVENTS
    def checkBtnClick(self, e):
        self.tc_info.SetValue("blahh")



    def check_all(self, e):
        self.read_all()






    #----------------------------------------------------------------------
    def redraw_pic(self, e):
        #blah
        print "Redraw called"

    def save_all(self, event):
        saveFileDialog = wx.FileDialog(self, "Save As", "", "","Python files (*.py)|*.py",wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
        saveFileDialog.ShowModal()
        saveFileDialog.GetPath()
        saveFileDialog.Destroy()

    def read_all(self):

        # self.ibox=0 #increases each time a new box is added
        # self.nbox=0 #number of boxes currently on the page (accounts for fact some may have been deleted)
        # self.Aid=[]
        # self.Apresent=[]

        self.l_hybrid=0
        self.l_colls=0
        self.l_implicit=0
        self.l_damp_bounds=0
        self.l_cold=0
        self.l_laser_const=0
        # self.nppc=self.tc_nppc.GetValue()
        self.nx=self.tc_nx.GetValue()
        self.nsp=self.tc_nsp.GetValue()
        self.xmax=self.tc_xmax.GetValue()
        self.nmin=self.tc_nmin.GetValue()
        self.inten=self.tc_inten.GetValue()
        self.Ti_init0=self.tc_Ti_init0.GetValue()
        self.Ti_init1=self.tc_Ti_init1.GetValue()
        self.mi=self.tc_mi.GetValue()
        self.tout=self.tc_tout.GetValue()
        self.gaussian_cutoffs=self.tc_gaussian_cutoffs.GetValue()
        self.t_FWHM=self.tc_t_FWHM.GetValue()


        print "nppc=",self.nppc
        print "t_FWHM=",self.t_FWHM

    # def add_region(self,e):
    #     #blah
    #     print "add called"

    def add_region(self,event):
        print "add_region called"

        # i=self.ibox
        i=region.nreg
        sizer=self.sizer2
        reg=region(Ani=0.0,AZ=0.0)
        self.Aregion_objects.append(reg)
        print "add_region:iw=",self.iw

        font_title = wx.Font(10, wx.DECORATIVE, wx.NORMAL, wx.BOLD)

        text_region = wx.StaticText(self.panel, label="Region "+str(i))
        text_region.SetFont(font_title)
        sizer.Add(text_region, pos=(self.iw, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM,border=15)
        self.iw+=1




if __name__ == '__main__':

    app = wx.App()
    MainWindow(None, title="")
    app.MainLoop()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 19:04:50

我并没有对此做太多的研究,但我可以通过添加以下内容来解决这个问题:

代码语言:javascript
复制
self.panel.Layout()
event.Skip()

end of add_region。之所以这样做,是因为当您向sizer添加内容时,必须告诉它布局已经更改,因此调用了self.panel.Layout。(注意这是对面板的调用,而不是对框架本身的调用)。event.Skip()告诉wx在某个事件类型启动时是否需要执行其他操作,在这种情况下,它会在按下按钮后检查是否还需要其他任何操作。

我应该说的一个快速注意是,最好将框架和Panel实例单独分离到一个类中。这样,面板就具有潜在的可重用性,以及更有组织的代码和最佳实践:p --如果您只是随意使用wx,那么这很好,但请注意:)

希望这能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29080847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档