[[oktatas:programozás:python:wxpython_gui|< wxPython GUI]]
====== wxPython könnyedén ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2020
* Licenc: GNU Free Documentation License 1.3
* Web: http://szit.hu
===== Kezdés =====
import wx
class MainFrame(wx.Frame):
pass
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
return True
app = App()
app.MainLoop()
===== Konstruktor létrehozása =====
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
super(MainFrame, self).__init__(parent)
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
return True
app = App()
app.MainLoop()
===== Két vezérlő elhelyezése =====
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
super(MainFrame, self).__init__(parent)
self.button1 = wx.Button(self, label='Mehet')
self.button1.SetPosition((50, 50))
self.entry = wx.TextCtrl(self)
self.entry.SetPosition((50, 100))
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
return True
app = App()
app.MainLoop()
===== Eseménykezelés =====
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
super(MainFrame, self).__init__(parent)
self.button1 = wx.Button(self, label='Mehet')
self.button1.SetPosition((50, 50))
self.Bind(wx.EVT_BUTTON, self.onClickGomb1, self.button1)
self.entry = wx.TextCtrl(self)
self.entry.SetPosition((50, 100))
def onClickGomb1(self, event):
print('Működik')
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
return True
app = App()
app.MainLoop()
===== Az esemény fejlesztése =====
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
super(MainFrame, self).__init__(parent)
self.button1 = wx.Button(self, label='vmi')
self.button1.SetPosition((50, 50))
self.Bind(wx.EVT_BUTTON, self.onClickGomb1, self.button1)
self.entry = wx.TextCtrl(self)
self.entry.SetPosition((50, 100))
def onClickGomb1(self, event):
numStr = self.entry.GetValue()
product = int(numStr) * 2
self.entry.SetValue(str(product))
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
return True
app = App()
app.MainLoop()
===== InitUI létrehozása =====
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
super(MainFrame, self).__init__(parent)
self.InitUI()
def InitUI(self):
self.button1 = wx.Button(self, label='vmi')
self.button1.SetPosition((50, 50))
self.Bind(wx.EVT_BUTTON, self.onClickGomb1, self.button1)
self.entry = wx.TextCtrl(self)
self.entry.SetPosition((50, 100))
def onClickGomb1(self, event):
numStr = self.entry.GetValue()
product = int(numStr) * 2
self.entry.SetValue(str(product))
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
return True
app = App()
app.MainLoop()
===== Méretező használata =====
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
super(MainFrame, self).__init__(parent)
self.InitUI()
self.InitLayout()
def InitUI(self):
self.button1 = wx.Button(self, label='vmi')
self.Bind(wx.EVT_BUTTON, self.onClickGomb1, self.button1)
self.entry = wx.TextCtrl(self)
def InitLayout(self):
self.sizer1 = wx.BoxSizer(wx.VERTICAL)
self.sizer1.Add(self.button1)
self.sizer1.Add(self.entry)
self.SetSizer(self.sizer1)
def onClickGomb1(self, event):
numStr = self.entry.GetValue()
product = int(numStr) * 2
self.entry.SetValue(str(product))
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
return True
app = App()
app.MainLoop()
===== Szétválasztás =====
import wx
from views.MainFrame import MainFrame
from controllers.Controller import Controller
class App(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show()
Controller(frame)
return True
app = App()
app.MainLoop()
import wx
class MainFrame(wx.Frame):
def __init__(self, parent):
super(MainFrame, self).__init__(parent)
self.InitUI()
self.InitLayout()
def InitUI(self):
self.button1 = wx.Button(self, label='vmi')
self.entry = wx.TextCtrl(self)
def InitLayout(self):
self.sizer1 = wx.BoxSizer(wx.VERTICAL)
self.sizer1.Add(self.button1)
self.sizer1.Add(self.entry)
self.SetSizer(self.sizer1)
import wx
from views.MainFrame import MainFrame
class Controller:
def __init__(self, mainFrame: MainFrame):
self.mainFrame = mainFrame
self.mainFrame.Bind(wx.EVT_BUTTON, self.onClickGomb1, self.mainFrame.button1)
def onClickGomb1(self, event):
numStr = self.mainFrame.entry.GetValue()
product = int(numStr) * 2
self.mainFrame.entry.SetValue(str(product))