■ Tkinter
from Tkinter import *
class Application(Frame):
def say_hi(self):
print "hi there, everyone!"
def createWidgets(self):
self.gui = Button(self)
self.gui["text"] = "QUIT"
self.gui["fg"] = "red"
self.gui["command"] = self.quit
self.gui.pack({"side": "left"})
self.o = Button(self)
self.o["text"] = "Hello",
self.o["command"] = self.say_hi
self.o.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application()
app.mainloop()
root.destroy()
■ wxPython
Window をだす
import wx
app = wx.App()
frame = wx.Frame(None, -1, "simple" )
frame.Show(True)
app.MainLoop()
GUI部品を配置する
app = wx.App()
frame = wx.Frame(None, -1, "simple" )
frame.Show(True)
wx.StaticText(frame, -1, "test", (10, 10))
wx.Button(frame, -1, "push", (10, 20))
app.MainLoop()
キーボードイベント
import wx
class Test(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
panel = wx.Panel(self, -1)
panel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
panel.SetFocus()
self.Show(True)
# keyboard Callback
def OnKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_ESCAPE:
ret = wx.MessageBox('Are you sure to quit?', 'Question',
wx.YES_NO | wx.NO_DEFAULT, self)
if ret == wx.YES:
self.Close()
event.Skip()
app = wx.App()
Test(None, -1, "keyevent" )
app.MainLoop()
ボタンイベント
import wx;
class MyButton(wx.Button):
def __init__(self, parent, id, label, pos):
wx.Button.__init__(self, parent, id, label, pos)
self.Bind(wx.EVT_BUTTON, self.OnClicked)
def OnClicked(self, event):
print 'event reached button class'
event.Skip()
class Propagate(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(250, 150))
# panel = MyPanel(self, -1)
MyButton(self, -1, 'Ok', (15, 15))
#self.Bind(wx.EVT_BUTTON, self.OnClicked)
self.Show(True)
def OnClicked(self, event):
print 'event reached frame class'
event.Skip()
app = wx.App()
Propagate(None, -1, "propagate" )
app.MainLoop()