• Как связать код Python и GUI на wxPython?

    @dim5x
    ЗИ, ИБ. Помогли? Поблагодарите. Отметьте ответом.
    6717cf333e689297739155.png
    Код
    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
            wx.Frame.__init__(self, *args, **kwds)
            self.SetSize((250, 250))
            self.SetTitle("frame")
    
            sizer_1 = wx.WrapSizer(wx.VERTICAL)
    
            fields = [
                ("Ширина (м)", "a"),
                ("Длина (м)", "b"),
                ("Высота (м)", "h"),
                ("Окно ширина (м)", "o1"),
                ("Окно высота (м)", "o2"),
                ("Дверь ширина (м)", "d1"),
                ("Дверь высота (м)", "d2"),
            ]
    
            # Create grid sizers and controls dynamically
            for label_text, attr in fields:
                grid_sizer = wx.GridSizer(1, 2, 0, 0)
                sizer_1.Add(grid_sizer, 1, wx.EXPAND, 0)
    
                label = wx.StaticText(self, wx.ID_ANY, label_text)
                grid_sizer.Add(label, 0, wx.ALL, 1)
    
                text_ctrl = wx.TextCtrl(self, wx.ID_ANY, "")
                setattr(self, attr, text_ctrl)  # Store the text control in the instance
                grid_sizer.Add(text_ctrl, 0, 0, 0)
    
            # Create a horizontal sizer for the result and button
            h_sizer = wx.BoxSizer(wx.HORIZONTAL)
    
            # Result text control
            self.itogo = wx.TextCtrl(self, wx.ID_ANY, "")
            self.itogo.SetBackgroundColour((171, 171, 171))
            h_sizer.Add(self.itogo, 1, wx.EXPAND | wx.ALL, 5)
    
            # Calculate button
            self.button_1 = wx.Button(self, wx.ID_ANY, "Посчитать", size=(110, 21))
            h_sizer.Add(self.button_1, 0, wx.ALL, 5)
            self.button_1.Bind(wx.EVT_BUTTON, self.onclick)
    
            # Add the horizontal sizer to the main sizer
            sizer_1.Add(h_sizer, 0, wx.EXPAND, 0)
    
            self.SetSizer(sizer_1)
            self.Layout()
    
        def onclick(self, event):
            a = float(self.a.GetValue())
            b = float(self.b.GetValue())
            h = float(self.h.GetValue())
            o1 = float(self.o1.GetValue())
            o2 = float(self.o2.GetValue())
            d1 = float(self.d1.GetValue())
            d2 = float(self.d2.GetValue())
    
            result = round(a * 2 * h + b * 2 * h - o1 * o2 - d1 * d2, 2)
            self.itogo.SetValue(str(result))
    
    class MyApp(wx.App):
        def OnInit(self):
            self.frame = MyFrame(None, wx.ID_ANY, "")
            self.SetTopWindow(self.frame)
            self.frame.Show()
            return True
    
    
    if __name__ == "__main__":
        app = MyApp(0)
        app.MainLoop()
    Ответ написан
    1 комментарий