はじめに
前回の続きです。touch-sp.hatenablog.com
今回はGUIを作りました。
環境
Windows 11 .NET framework 4.8 Newtonsoft.Json 13.0.3 (from NuGet)
C#コード
まずはボタンとテキストボックスがセットになったユーザーコントロールを作りました。using System; using System.Windows.Forms; namespace speech2text { public class ButtonTextbox : UserControl { private TableLayoutPanel tlp; private Button button; private TextBox textbox; public event EventHandler ButtonClicked; public ButtonTextbox() { InitializeComponet(); } private void InitializeComponet() { tlp = new TableLayoutPanel(); button = new Button(); button.Anchor = AnchorStyles.Left | AnchorStyles.Right; button.Click += (sender, e) => { ButtonClicked?.Invoke(this, EventArgs.Empty); }; textbox = new TextBox(); textbox.Anchor = AnchorStyles.Left | AnchorStyles.Right; Dock = DockStyle.Fill; // TableLayoutPanel Settings tlp.RowCount = 1; tlp.ColumnCount = 2; tlp.Dock = DockStyle.Fill; tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F)); tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80F)); tlp.Controls.Add(button, 0, 0); tlp.Controls.Add(textbox, 1, 0); Controls.Add(tlp); } public string ButtonText { get { return button.Text; } set { button.Text = value; } } public string TextBoxText { get { return textbox.Text; } set { textbox.Text = value; } } } }
メインコード
using System.Diagnostics; using System.Drawing; using System.Windows.Forms; using System.IO; using Newtonsoft.Json; using System.Collections.Generic; namespace speech2text { public partial class Form1 : Form { private TableLayoutPanel general; private ButtonTextbox button_textbox_1; private ButtonTextbox button_textbox_2; private ButtonTextbox button_textbox_3; private Button exe_button; private TextBox result_textbox; public Form1() { InitializeComponent(); form_initilaize(); } private void form_initilaize() { button_textbox_1 = new ButtonTextbox(); button_textbox_1.ButtonText = "python interpreter"; button_textbox_1.ButtonClicked += (sender, e) => { var dialog = new OpenFileDialog(); dialog.Filter = "Python実行ファイル (*.exe)|*.exe"; if(dialog.ShowDialog() == DialogResult.OK) { button_textbox_1.TextBoxText = dialog.FileName; } }; button_textbox_2 = new ButtonTextbox(); button_textbox_2.ButtonText = "execute file path"; button_textbox_2.ButtonClicked += (sender, e) => { var dialog = new OpenFileDialog(); dialog.Filter = "Pythonスクリプト (*.py)|*.py"; if (dialog.ShowDialog() == DialogResult.OK) { button_textbox_2.TextBoxText = dialog.FileName; } }; button_textbox_3 = new ButtonTextbox(); button_textbox_3.ButtonText = "audio file path"; button_textbox_3.ButtonClicked += (sender, e) => { var dialog = new OpenFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { button_textbox_3.TextBoxText = dialog.FileName; } }; exe_button = new Button(); exe_button.Anchor = AnchorStyles.Left | AnchorStyles.Right; exe_button.Text = "execute!!"; exe_button.Click += (sender, e) => { if (string.IsNullOrEmpty(button_textbox_1.TextBoxText)) { MessageBox.Show("Pythonインタプリタを選択して下さい"); return; } if (string.IsNullOrEmpty(button_textbox_2.TextBoxText)) { MessageBox.Show("Pythonスクリプトを選択して下さい"); return; } if (string.IsNullOrEmpty(button_textbox_3.TextBoxText)) { MessageBox.Show("音声ファイルを選択して下さい"); return; } result_textbox.Text = ""; var startInfo = new ProcessStartInfo { FileName = button_textbox_1.TextBoxText, Arguments = button_textbox_2.TextBoxText + " " + button_textbox_3.TextBoxText, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; using (var process = new Process { StartInfo = startInfo }) { process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); result_textbox.Text = output; } }; general = new TableLayoutPanel(); general.Dock = DockStyle.Fill; general.RowCount = 5; general.ColumnCount = 1; general.RowStyles.Add(new RowStyle(SizeType.Percent, 8F)); general.RowStyles.Add(new RowStyle(SizeType.Percent, 8F)); general.RowStyles.Add(new RowStyle(SizeType.Percent, 8F)); general.RowStyles.Add(new RowStyle(SizeType.Percent, 8F)); general.RowStyles.Add(new RowStyle(SizeType.Percent, 76F)); result_textbox = new TextBox(); result_textbox.Multiline = true; result_textbox.Dock = DockStyle.Fill; result_textbox.Font = new Font("游ゴシック", 11); general.Controls.Add(button_textbox_1, 0, 0); general.Controls.Add(button_textbox_2, 0, 1); general.Controls.Add(button_textbox_3, 0, 2); general.Controls.Add(exe_button, 0, 3); general.Controls.Add(result_textbox, 0, 4); Controls.Add(general); if (File.Exists("settings.json")) { string jsonFromFile = File.ReadAllText("settings.json"); Dictionary<string, string> loadedDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonFromFile); loadedDictionary.TryGetValue("interpreter", out string interpreter); loadedDictionary.TryGetValue("script", out string script); if (!string.IsNullOrEmpty(interpreter)) { button_textbox_1.TextBoxText = interpreter; } if (!string.IsNullOrEmpty(script)) { button_textbox_2.TextBoxText = script; } } } } }
「settings.json」を用意しておけば読み込んでくれます。
{ "interpreter":"D:\\reazonspeech\\env\\py311\\Scripts\\python.exe", "script":"D:\\reazonspeech\\run.py" }