VBA版をPython版にしてみた。
ChatGPTにVBA版のソースコードをPythonに変換してくれと頼んだら一発で成功!
Bardの登場がChatGPTに大きな影響を与えているのが実感できる。
使い物にならなかったのが全く別物といってよい程の改善が行われたようだ。
import tkinter as tk
from tkinter import messagebox
def create_line():
start_x = int(tk.simpledialog.askstring("直線", "始点のX座標を入力してください。"))
start_y = int(tk.simpledialog.askstring("直線", "始点のY座標を入力してください。"))
end_x = int(tk.simpledialog.askstring("直線", "終点のX座標を入力してください。"))
end_y = int(tk.simpledialog.askstring("直線", "終点のY座標を入力してください。"))
canvas.create_line(start_x, start_y, end_x, end_y, width=2)
def create_circle():
center_x = int(tk.simpledialog.askstring("円", "中心のX座標を入力してください。"))
center_y = int(tk.simpledialog.askstring("円", "中心のY座標を入力してください。"))
radius = int(tk.simpledialog.askstring("円", "半径を入力してください。"))
x1 = center_x - radius
y1 = center_y - radius
x2 = center_x + radius
y2 = center_y + radius
canvas.create_oval(x1, y1, x2, y2, width=2, outline="red")
def process_user_input():
user_input = tk.simpledialog.askstring("CAD", "何をしましょうか?")
has_line = "直線" in user_input
has_circle = "円" in user_input
if has_line:
create_line()
elif has_circle:
create_circle()
else:
tk.messagebox.showerror("エラー", "そのような操作はできません。")
root = tk.Tk()
root.title("CAD")
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()
button = tk.Button(root, text="操作を入力", command=process_user_input)
button.pack()
root.mainloop()
もちろんこれは簡易プロトタイプである。
こんなもん現場では全然実用にならない。もっと複雑な操作が要求される。
次の段階はAutocadにこの機能を組み込むことが目標である。
それが成功したら、どんどんいろんな操作・機能を追加していこうと思っている。
Autolispのソースコード内でPythonを呼び出して組み込み、自動で描画できれば開発が早まるだろう。
もしそれができなければ、Autolispで全て書かなければならない。
はたしてAutolisp言語にはチャットボットを作るための機能や関数はあるのだろうか?
Lisp言語の本では人工知能について言及があったからAutolispでも可能なのではないかと考えているが・・・
import tkinter as tk
import qrcode
def generate_qrcode():
python_code = code_entry.get("1.0", tk.END)
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(python_code)
qr.make(fit=True)
filename = filename_entry.get()
image = qr.make_image(fill_color="black", back_color="white")
image.save(filename)
# TKinterウィンドウの作成
window = tk.Tk()
window.title("QR Code Generator")
# テキスト入力フォーム
code_label = tk.Label(window, text="Python Code:")
code_label.pack()
code_entry = tk.Text(window, height=10, width=50)
code_entry.pack()
# ファイル名入力フォーム
filename_label = tk.Label(window, text="Filename:")
filename_label.pack()
filename_entry = tk.Entry(window)
filename_entry.pack()
# 保存ボタン
generate_button = tk.Button(window, text="Generate and Save QR Code", command=generate_qrcode)
generate_button.pack()
# TKinterウィンドウの実行
window.mainloop()
↑このQRコードを読み取るとwww


https://blog.goo.ne.jp/nichikon2/e/bc53b832fb46b2466196b41cc1cdf2ff
の記事で作ったプログラムをちょっと改造してみた。
ユーザーが入力した文字の中に「円」または「直線」が含まれているかどうか判定し、
もし含まれていた場合のみ直線または円を描くプログラムを実行するようにした。
ちなみにbardでは何回も修正するように依頼したのだが、
全く直らないどころか同じ返答を繰り返すばかりだったので、
ChatGPTに頼んでみたら、一発で直った。
それが次のコードである。
Sub CAD()
Dim userInput As String
Dim hasLine As Boolean
Dim hasCircle As Boolean
userInput = InputBox("何をしましょうか?")
hasLine = InStr(userInput, "直線") > 0
hasCircle = InStr(userInput, "円") > 0
If hasLine Then
MsgBox "始点と終点の座標を指定してください。"
Dim StartX As Integer
Dim StartY As Integer
Dim EndX As Integer
Dim EndY As Integer
StartX = InputBox("始点のX座標を入力してください。")
StartY = InputBox("始点のY座標を入力してください。")
EndX = InputBox("終点のX座標を入力してください。")
EndY = InputBox("終点のY座標を入力してください。")
With ActiveSheet.Shapes.AddLine(StartX, StartY, EndX, EndY)
.Line.Weight = 2 ' Set the line thickness to 2 points
End With
ElseIf hasCircle Then
MsgBox "中心の座標と半径を入力してください。"
Dim centerX As Variant
Dim centerY As Variant
Dim radius As Variant
Dim drawingSheet As Worksheet
Dim newCircle As Shape
Set drawingSheet = ActiveSheet
centerX = Application.InputBox("Enter the x-coordinate of the center of the circle", "Center X", Type:=1)
centerY = Application.InputBox("Enter the y-coordinate of the center of the circle", "Center Y", Type:=1)
radius = Application.InputBox("Enter the radius of the circle", "Radius", Type:=1)
Set newCircle = drawingSheet.Shapes.AddShape(msoShapeOval, _
-radius, centerY - radius, radius * 2, radius * 2)
With newCircle.Line
.Weight = 2 ' Set the line thickness to 2 points
.DashStyle = msoLineSolid ' Set the line style to solid
.ForeColor.RGB = RGB(255, 0, 0) ' Set the line color to red
End With
newCircle.Fill.Visible = msoFalse ' Set the fill to transparent
Else
MsgBox "そのような操作はできません。"
End If
End Sub
てなわけで今回はChatGPTの勝ち。
MQL4でPythonを呼び出す方法
MQL4 から Python を呼び出す方法はいくつかあります。1 つの方法は、Python プログラムをコンパイルして DLL ファイルにしてから、MQL4 から DLL ファイルをロードすることです。もう 1 つの方法は、Python スクリプトを実行するサーバーを使用して、MQL4 から Python スクリプトを呼び出すことです。
DLL ファイルを使用して MQL4 から Python を呼び出すには、次の手順に従います。
Python プログラムをコンパイルして DLL ファイルにします。
DLL ファイルを MQL4 のインストールディレクトリにコピーします。
MQL4 のソースファイルで、次の行を使用して DLL ファイルをロードします。
#include
HMODULE hModule = LoadLibrary("python.dll");
if (hModule == NULL) {
// エラー処理
}
DLL 内の関数を呼び出すには、次の構文を使用します。
FARPROC pFunc = GetProcAddress(hModule, "FunctionName");
if (pFunc == NULL) {
// エラー処理
}
int result = (int)pFunc();
サーバーを使用して MQL4 から Python スクリプトを呼び出すには、次の手順に従います。
Python スクリプトを実行するサーバーをセットアップします。
MQL4 のソースファイルで、次の行を使用してサーバーに接続します。
#include
#include
using namespace std;
int main() {
// サーバーに接続
string host = "localhost";
int port = 5000;
Socket socket(host, port);
if (!socket.isConnected()) {
// エラー処理
}
// Python スクリプトを実行する
string command = "python myscript.py";
socket.write(command.c_str());
// 応答を待つ
char buffer[1024];
int bytesReceived = socket.read(buffer, 1024);
if (bytesReceived == -1) {
// エラー処理
}
// 応答を出力する
cout <
// サーバーを切断する
socket.close();
return 0;
}
最近発表されたgoogle のAIチャットで質問をしてみた。
VBAでチャットボットを作りたいと。
早速ソースコードの例が生成されたが、
Bard AI 「What would you like to talk about?」
俺様「hello」の場合、Bard AI「Hello there!」
俺様「goodbye」の場合、Bard AI「Goodbye!」
俺様「上記以外」の場合、Bard AI「I don't understand. Please try again.」
というショボいものだった。
あまりにもショボいので改造してみた。
Sub ChatBot()
Dim userInput As String
userInput = InputBox("何をしましょうか?")
Select Case userInput
Case "直線"
MsgBox "始点と終点の座標を指定してください。"
Dim StartX As Integer
Dim StartY As Integer
Dim EndX As Integer
Dim EndY As Integer
StartX = InputBox("始点のX座標を入力してください。")
StartY = InputBox("始点のY座標を入力してください。")
EndX = InputBox("終点のX座標を入力してください。")
EndY = InputBox("終点のY座標を入力してください。")
With ActiveSheet.Shapes.AddLine(StartX, StartY, EndX, EndY)
.Line.Weight = 2 ' Set the line thickness to 2 points
End With
Case "円"
MsgBox "中心の座標と半径を入力してください。"
Dim centerX As Variant
Dim centerY As Variant
Dim radius As Variant
Dim drawingSheet As Worksheet
Dim newCircle As Shape
Set drawingSheet = ActiveSheet
centerX = Application.InputBox("Enter the x-coordinate of the center of the circle", "Center X", Type:=1)
centerY = Application.InputBox("Enter the y-coordinate of the center of the circle", "Center Y", Type:=1)
radius = Application.InputBox("Enter the radius of the circle", "Radius", Type:=1)
Set newCircle = drawingSheet.Shapes.AddShape(msoShapeOval, _
-radius, centerY - radius, radius * 2, radius * 2)
With newCircle.Line
.Weight = 2 ' Set the line thickness to 2 points
.DashStyle = msoLineSolid ' Set the line style to solid
.ForeColor.RGB = RGB(255, 0, 0) ' Set the line color to red
End With
newCircle.Fill.Visible = msoFalse ' Set the fill to transparent
Case Else
MsgBox "そのような操作はできません。"
End Select
End Sub
結果はバッチリ!
次はセレクトボックスを表示させ、やりたい操作を選ばせて、数字を入力する形式にしてみたい。




…失敗したようだ。