4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonによる単純なUDP送信GUIプログラム

Posted at

概要

UDPパケットを送信するプログラムをGUI化したい
GUIの構築にはTkinterを使う

環境

  • OS : Windows10 Pro
  • IDE : PyCharm CE
  • Python3.6

GUI

下の画像みたいな感じにする
Sendだけはボタンで他はテキストボックス
テキストボックスの実装には一行だけ入力できるtk.Entryを使う
gui.png

  • IP : 対象のIPアドレス
  • Port : 対象のポート
  • Delay : パケットを送る間隔
  • Data : パケットの中身
  • Num : 送るパケットの総数

ソース

udp_send_gui.py
# -*- coding:utf-8 -*-

import time
import socket
import tkinter as tk


def udp_send():
    msg = DataBox.get()
    # 送るメッセージ

    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    delay_time = DelayBox.get()

    for i in range(0, int(NumBox.get())):
        client.sendto(msg.encode(), (IpBox.get(), int(PortBox.get())))
        time.sleep(float(delay_time))
# UDP送信関数


root = tk.Tk()  # ウィンドウ作成
root.title("UDP Packet Generator")  # ウィンドウタイトル
root.geometry("640x480")    # ウィンドウサイズ


Ip = tk.Label(root, text="IP:")
Port = tk.Label(root, text="Port:")
Delay = tk.Label(root, text="Delay:")
Data = tk.Label(root, text="Data:")
Num = tk.Label(root, text="Num:")

IpBox = tk.Entry(font=("Consolas", "30"))
PortBox = tk.Entry(font=("Consolas", "30"))
DelayBox = tk.Entry(font=("Consolas", "30"))
DataBox = tk.Entry(font=("Consolas", "30"))
NumBox = tk.Entry(font=("Consolas", "30"))

IpBox.insert(tk.END, "192.168.")
PortBox.insert(tk.END, "9998")
DelayBox.insert(tk.END, "0.1")
NumBox.insert(tk.END, "1")
# 部品の作成


Ip.place(height=60, y=30)
Port.place(height=60, y=150)
Delay.place(height=60, y=270)
Data.place(height=60, y=390)
Num.place(height=60, x=440, y=390)

IpBox.place(height=60, width=360, x=60, y=30)
PortBox.place(height=60, width=360, x=60, y=150)
DelayBox.place(height=60, width=360, x=60, y=270)
DataBox.place(height=60, width=360, x=60, y=390)
NumBox.place(height=60, width=120, x=500, y=390)
# 部品の配置


Send = tk.Button(root, text="Send!")
Send["command"] = udp_send
Send.place(height=300, width=185, x=440, y=30)
# 送信ボタン

root.mainloop()

結果

UDP_Packet_Generator.PNG

まとめ

簡単にGUIが作れるTkinterつよい

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?