PythonでGUI。ボタン押したらグラフが開く
- hiro2studio
- 2021年2月20日
- 読了時間: 1分
GUIにグラフ領域を埋め込むのではなく、matplotlibの画面を別で開く
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as pt
import wx
import numpy as np
import random
class TestGUI(wx.Frame):
def __init__(self, parent, id=-1, title='TEST GUI'):
wx.Frame.__init__(self, parent, id, title)
self.SetTitle('TEST GUI')
self.SetSize((300, 100))
panel = wx.Panel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
# ボタンの作成
self.Btn = wx.Button(panel, -1, label="push",size=(60, 20))
# ボタンを割り当て
self.Bind(wx.EVT_BUTTON, self.push, self.Btn)
# ラベルの作成
self.label = wx.StaticText(panel, -1, ' ')
self.sizer.Add(self.Btn)
self.sizer.Add(self.label)
panel.SetSizer(self.sizer)
panel.Fit()
# Plop
self.ppp = pt;
self.fig = self.ppp.figure()
self.axes = self.fig.add_subplot(1, 1, 1)
self.Show(True)
def push(self, event):
self.label.SetLabel('OK')
self.ppp.cla()
# int型の0で初期化
fig1 = np.zeros((10, 10, 2), dtype=np.int)
fig2 = np.zeros((10, 10, 2), dtype=np.int)
for i in range(0, 10):
for j in range(0, 10):
fig1[i][j][0] = j * 10
fig1[i][j][1] = i * 10
fig2[i][j][0] = j * 11
fig2[i][j][1] = i * 11
self.axes.scatter(fig1[i][j][0], fig1[i][j][1], c='red')
self.axes.scatter(fig2[i][j][0], fig2[i][j][1], c='c')
self.axes.grid(True)
self.ppp.show()
#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':
app=wx.App()
TestGUI(None, wx.ID_ANY, "Btn")
app.MainLoop()
matplotlibの❌ボタンで閉じた後再度ボタン押しても動かないので対応は後ほど
Comments