如何在新窗口中输入值并将wx.newindow的值传递给wx.Frame wxpython

时间:2019-03-11 08:47:52

标签: dialog window wxpython python-3.6

我有一个带有动态按钮的面板,当我单击按钮时,我有一个新窗口打开了一个问题,我需要区域来输入值来编辑动态图像的参数,例如

enter image description here

那是我的代码:

import wx

class MainFrame(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
        self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
        self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
        self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
        st1 = wx.StaticText(self.bottom, -1, "show info ")
        self.bottom.SetBackgroundColour('white')
        dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
        self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        sizer1.Add(self.top,1,wx.EXPAND,5)
        sizer1.Add(self.bottom,1,wx.EXPAND,5)

        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer2.Add(self.left,0,wx.EXPAND,5)
        sizer2.Add(sizer1,1,wx.EXPAND,5)
        self.SetSizer(sizer2)

    def newwindow(self, event):
        secondWindow = window2(parent=self.left)
        secondWindow.Show()

class window2(wx.Frame):

    title = "new Window"

    def __init__(self,parent):
        wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
        panel=wx.Panel(self, -1)

        self.SetBackgroundColour(wx.Colour(100,100,100))
        self.Centre()
        self.Show()

app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()

如何添加区域以编辑图片等参数? 我不确定newwindow是否是我需要的或对话框!

感谢帮助

1 个答案:

答案 0 :(得分:1)

我想您在正常的新窗口中会满意。您可以使用wx.TextCtrl小部件来获取写入参数的区域。您将需要一种导出键入到wx.TextCtrl中的值的方法,因此我添加了样式wx.TE_PROCESS_ENTER。使用这种样式,当您完成键入并按Enter时,可以处理键入的值。

此外,也不需要两次使用Show()secondWindow.Show()self.Show())。其中之一就足够了。

带注释的代码:

import wx

class MainFrame(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
        self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
        self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
        self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
        st1 = wx.StaticText(self.bottom, -1, "show info ")
        self.bottom.SetBackgroundColour('white')
        dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
        self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        sizer1.Add(self.top,1,wx.EXPAND,5)
        sizer1.Add(self.bottom,1,wx.EXPAND,5)

        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer2.Add(self.left,0,wx.EXPAND,5)
        sizer2.Add(sizer1,1,wx.EXPAND,5)
        self.SetSizer(sizer2)

    def newwindow(self, event):
        secondWindow = window2(parent=self.left)
        secondWindow.Show()

class window2(wx.Frame):

    title = "new Window"

    def __init__(self,parent):
        """
        This is similar to the class MainFrame. You define a parent wx.Panel
        and all other widgets are his childs. 
        """
        wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
        self.panel=wx.Panel(self, -1, style=wx.SUNKEN_BORDER)

        self.st = wx.StaticText(self.panel, label='modifier bornes de la dynamique', style=wx.ALIGN_CENTER)
        #### Notice the wx.TE_PROCESS_ENTER style to trigger processing the input when
        #### Enter is pressed. Another alternative is to put a button somewhere.
        self.text = wx.TextCtrl(self.panel, size=(200, 20), style=wx.SUNKEN_BORDER|wx.TE_PROCESS_ENTER)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.st, 0, wx.EXPAND|wx.ALL, 5)
        self.sizer.Add(self.text, 0, wx.ALIGN_CENTER|wx.ALL, 5)

        self.panel.SetSizer(self.sizer)
        self.sizer.Fit(self.panel)

        #self.SetBackgroundColour(wx.Colour(100,100,100))
        self.Centre()
        #### No need to use Show() here since you already use it in MainFrame.newwindow()
        self.Show()
        #### To execute self.onEnter when Enter is pressed inside self.text
        self.Bind(wx.EVT_TEXT_ENTER, self.onEnter)

    def onEnter(self, event):
        #### Change it to fit your needs
        print(self.text.GetValue())
        self.Destroy()

app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()