Strumenti Utente

Strumenti Sito


Barra laterale

wxpython

Hello world in Wxpython

Autore: Fabio Di Matteo
Ultima revisione: 03/01/2019 - 19:34

hello.py

#!/usr/bin/env python3
import wx
from wx import xrc
 
class MyApp(wx.App):
 
    def OnInit(self):
        self.res = xrc.XmlResource('gui.xrc')
        self.init_frame()
        return True
 
    def init_frame(self):
        self.frame = self.res.LoadFrame(None, 'mainFrame')
        self.panel = xrc.XRCCTRL(self.frame, 'panel')
        self.text1 = xrc.XRCCTRL(self.panel, 'text1')
        self.text2 = xrc.XRCCTRL(self.panel, 'text2')
 
        self.text1.SetValue("Ciao mondo!")
        self.frame.Bind(wx.EVT_BUTTON, self.OnSubmit, id=xrc.XRCID('button'))
        self.frame.Show()
 
    def OnSubmit(self, evt):
        wx.MessageBox('Your name is %s %s!' %
            (self.text1.GetValue(), self.text2.GetValue()), 'Feedback')
 
 
if __name__ == '__main__':
    app = MyApp(False)
    app.MainLoop()

gui.xrc

<?xml version="1.0" encoding="utf-8"?>
<!-- design layout in a separate XML file -->
<resource>
  <object class="wxFrame" name="mainFrame">
    <title>My Frame</title>
    <object class="wxPanel" name="panel">
      <object class="wxFlexGridSizer">
        <cols>2</cols>
        <rows>3</rows>
        <vgap>5</vgap>
        <hgap>5</hgap>
        <object class="sizeritem">
          <object class="wxStaticText" name="label1">
            <label>First name:</label>
          </object>
        </object>
        <object class="sizeritem">
          <object class="wxTextCtrl" name="text1"/>
        </object>
        <object class="sizeritem">
          <object class="wxStaticText" name="label2">
            <label>Last name:</label>
          </object>
        </object>
        <object class="sizeritem">
          <object class="wxTextCtrl" name="text2"/>
        </object>
        <object class="spacer">
          <size>0,0</size>
        </object>
        <object class="sizeritem">
          <object class="wxButton" name="button">
            <label>Submit</label>
          </object>
        </object>
      </object>
    </object>
  </object>
</resource>

Gestire i menu da file xrc

La procedura è un po' diversa ma assomiglia a quella dei widgets:

                #Getting menu
		self.menubar = self.frame.GetMenuBar()
		self.mnuItemExit = self.menubar.FindItemById(xrc.XRCID('mnuItemExit'))
		self.mnuItemNewProfile = self.menubar.FindItemById(xrc.XRCID('mnuItemNewProfile'))    
		#Bind event Menu
		self.frame.Bind(wx.EVT_MENU, self.quit, self.mnuItemExit)
		self.frame.Bind(wx.EVT_MENU, self.openProfiles, self.mnuItemNewProfile)

Freezare la nostra app

Usando cx_freeze possiamo usare il seguente script.

make.py

import sys
from cx_Freeze import setup, Executable
 
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
 
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"
 
setup(  name = "guifoo",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("hello.py", base=base)])

wxpython.txt · Ultima modifica: 16/01/2019 - 17:57 da Fabio Di Matteo