Skip to content

Commit 4e98d4b

Browse files
committed
update
1 parent 259bd16 commit 4e98d4b

File tree

6 files changed

+138
-254
lines changed

6 files changed

+138
-254
lines changed

about.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
1-
"""MQTT Client GUI
2-
1+
"""
2+
MQTT Client GUI - About Window
33
Author: Sahin MERSIN - electrocoder <[email protected]>
4-
5-
Source Code: https://github.com/electrocoder/MQTTClient
6-
7-
MQTT Examples: https://github.com/meseiot/iot-examples
8-
9-
Date: 12.11.2022
10-
11-
File: This script is About window
124
"""
5+
136
import os
147
import tkinter as tk
8+
from tkinter import ttk
159

1610

1711
class AboutWindow(tk.Toplevel):
18-
def __init__(self, main_window, font_size):
12+
def __init__(self, main_window):
1913
super().__init__(main_window)
20-
21-
self.title("MQTT Client About")
14+
self.title("About MQTT Client")
2215
self.geometry('400x350')
16+
self.transient(main_window)
17+
self.grab_set()
2318

24-
ipadding = {'ipadx': 1, 'ipady': 1}
19+
text = tk.Text(self, font=("Helvetica", 12), height=10)
20+
text.pack(padx=10, pady=10, expand=True, fill=tk.BOTH)
2521

26-
text1 = tk.Text(self, font=font_size, height=10)
27-
text1.pack(**ipadding, side=tk.TOP, expand=True, fill=tk.BOTH)
28-
29-
button1 = tk.Button(self, text='OK', font=font_size,
30-
command=self.close)
31-
button1.pack(**ipadding, side=tk.TOP, expand=True, fill=tk.BOTH)
22+
ttk.Button(self, text='OK', command=self.destroy).pack(pady=5)
3223

3324
basedir = os.path.dirname(__file__)
34-
self.file_name = os.path.join(basedir, "README.md")
35-
with open(self.file_name) as f:
36-
text1.insert(tk.INSERT, f.read())
37-
38-
def close(self):
39-
self.destroy()
25+
file_name = os.path.join(basedir, "README.md")
26+
try:
27+
with open(file_name, 'r') as f:
28+
text.insert(tk.INSERT, f.read())
29+
except FileNotFoundError:
30+
text.insert(tk.INSERT, "README.md file not found.\n\nMQTT Client by Sahin Mersin\nVersion: 0v5")

config_file.ini

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
[iothook]
2-
broker = iothook.com
3-
port = 1883
4-
username = iothookpublic
5-
password = iothookpublic
6-
topics = #,timestamp,test,testtopic,aaa,$SYS,
7-
81
[hivemq]
92
broker = broker.hivemq.com
103
port = 1883
@@ -38,7 +31,7 @@ broker = test.mosquitto.org
3831
port = 1883
3932
username =
4033
password =
41-
topics = #,timestamp,test,
34+
topics = #,timestamp,
4235

4336
[flespi]
4437
broker = mqtt.flespi.io
@@ -61,10 +54,24 @@ username =
6154
password =
6255
topics = #,timestamp,
6356

57+
[iothook]
58+
broker = iothook.com
59+
port = 1883
60+
username = iothookpublic
61+
password = iothookpublic
62+
topics = #,timestamp,test,testtopic,aaa,$SYS,
63+
6464
[ele]
6565
broker = iothook.com
6666
port = 1883
6767
username = iothookpublic
6868
password = iothookpublic
6969
topics = #,timestamp,test,testtopic,aaa,$SYS,
7070

71+
[keyubu]
72+
broker = 87.248.157.85
73+
port = 1884
74+
username =
75+
password =
76+
topics = #,testtopic,test,
77+

config_file.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
1-
"""MQTT Client GUI
2-
1+
"""
2+
MQTT Client GUI - Config File Operations
33
Author: Sahin MERSIN - electrocoder <[email protected]>
4-
5-
Source Code: https://github.com/electrocoder/MQTTClient
6-
7-
MQTT Examples: https://github.com/meseiot/iot-examples
8-
9-
Date: 12.11.2022
10-
11-
File: This script is Config file operations
124
"""
135

14-
import configparser
156
import os
16-
from os.path import exists
7+
import configparser
178

189

1910
class ConfigFile:
2011
def __init__(self):
2112
self.config = configparser.ConfigParser()
22-
2313
basedir = os.path.dirname(__file__)
2414
self.file_name = os.path.join(basedir, "config_file.ini")
25-
if exists(self.file_name):
15+
if os.path.exists(self.file_name):
2616
self.config.read(self.file_name)
2717

2818
def read_sections(self):
@@ -36,10 +26,8 @@ def create_file(self, name, broker, port, username, password):
3626
self.config.set(name, 'username', username)
3727
self.config.set(name, 'password', password)
3828
self.config.set(name, 'topics', '#,')
39-
4029
with open(self.file_name, 'w') as configfile:
4130
self.config.write(configfile)
42-
4331
return True
4432

4533
def read_broker(self, name):
@@ -48,32 +36,23 @@ def read_broker(self, name):
4836
port = self.config[name]["port"]
4937
username = self.config[name]["username"]
5038
password = self.config[name]["password"]
51-
5239
return name, broker, port, username, password
5340

5441
def read_topics(self, name):
5542
self.config.read(self.file_name)
56-
topics = self.config[name]["topics"]
57-
58-
return topics
43+
return self.config[name]["topics"]
5944

6045
def create_topic(self, name, topic):
61-
print("name", name)
62-
print("topic", topic)
6346
con = self.config[name]
6447
con['topics'] += topic + ","
65-
6648
with open(self.file_name, 'w') as configfile:
6749
self.config.write(configfile)
68-
6950
return topic
7051

7152
def delete(self, name):
7253
self.config.read(self.file_name)
73-
delete = self.config.remove_section(name)
74-
if delete:
54+
if self.config.remove_section(name):
7555
with open(self.file_name, 'w') as configfile:
7656
self.config.write(configfile)
7757
return True
78-
else:
79-
return False
58+
return False

new_connect.py

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,45 @@
1-
"""MQTT Client GUI
2-
1+
"""
2+
MQTT Client GUI - New MQTT Broker Connection Window
33
Author: Sahin MERSIN - electrocoder <[email protected]>
4-
5-
Source Code: https://github.com/electrocoder/MQTTClient
6-
7-
MQTT Examples: https://github.com/meseiot/iot-examples
8-
9-
Date: 12.11.2022
10-
11-
File: This script is New MQTT Broker
124
"""
135

14-
from tkinter import *
15-
6+
import tkinter as tk
7+
from tkinter import ttk
168
from config_file import ConfigFile
179

1810

19-
class NewConnect(Toplevel):
20-
def __init__(self, main_window, font_size):
11+
class NewConnect(tk.Toplevel):
12+
def __init__(self, main_window):
2113
super().__init__(main_window)
22-
2314
self.main_window = main_window
24-
self.title("MQTT Client New Connect")
25-
self.geometry("300x350")
26-
27-
ipadding = {'ipadx': 5, 'ipady': 5}
28-
29-
frame = Frame(self, padx=5, pady=5)
30-
frame.grid(row=0, column=1)
31-
32-
Label(frame, text='Name').pack(**ipadding)
33-
Label(frame, text='Broker').pack(**ipadding)
34-
Label(frame, text='Port').pack(**ipadding)
35-
Label(frame, text='Username').pack(**ipadding)
36-
Label(frame, text='Password').pack(**ipadding)
37-
38-
frame2 = Frame(self, padx=5, pady=5)
39-
frame2.grid(row=0, column=2)
40-
41-
self.entry_name_text = StringVar(self)
42-
self.entry_name = Entry(frame2, font=font_size,
43-
textvariable=self.entry_name_text).pack(**ipadding)
44-
45-
self.entry_broker_text = StringVar(self)
46-
self.entry_broker = Entry(frame2, font=font_size,
47-
textvariable=self.entry_broker_text).pack(**ipadding)
48-
49-
self.entry_port_text = StringVar(self)
50-
self.entry_port = Entry(frame2, font=font_size,
51-
textvariable=self.entry_port_text).pack(**ipadding)
52-
53-
self.entry_username_text = StringVar(self)
54-
self.entry_username = Entry(frame2, font=font_size,
55-
textvariable=self.entry_username_text).pack(
56-
**ipadding)
57-
58-
self.entry_password_text = StringVar(self)
59-
self.entry_password = Entry(frame2, font=font_size,
60-
textvariable=self.entry_password_text).pack(
61-
**ipadding)
62-
63-
self.button_cancel = Button(self, text="Cancel",
64-
font=font_size,
65-
command=self.cancel, padx=10).grid(row=1,
66-
column=1,
67-
pady=5)
68-
69-
self.button_save = Button(self, text="Save",
70-
font=font_size,
71-
command=self.save_config, padx=10).grid(
72-
row=1, column=2, pady=5)
15+
self.title("New Connection")
16+
self.geometry("300x300")
17+
self.transient(main_window)
18+
self.grab_set()
19+
20+
font = ("Helvetica", 12)
21+
frame = ttk.Frame(self, padding="10")
22+
frame.pack(fill="both", expand=True)
23+
24+
labels = ["Name", "Broker", "Port", "Username", "Password"]
25+
self.entries = {}
26+
for idx, label in enumerate(labels):
27+
ttk.Label(frame, text=f"{label}:").grid(row=idx, column=0, padx=5, pady=5, sticky="e")
28+
entry = ttk.Entry(frame, font=font)
29+
entry.grid(row=idx, column=1, padx=5, pady=5, sticky="ew")
30+
self.entries[label.lower()] = entry
31+
32+
ttk.Button(frame, text="Save", command=self.save_config).grid(row=5, column=1, pady=10, sticky="e")
33+
ttk.Button(frame, text="Cancel", command=self.destroy).grid(row=5, column=0, pady=10, sticky="w")
7334

7435
def save_config(self):
75-
ConfigFile().create_file(self.entry_name_text.get(),
76-
self.entry_broker_text.get(),
77-
self.entry_port_text.get(),
78-
self.entry_username_text.get(),
79-
self.entry_password_text.get())
80-
self.destroy()
81-
82-
def cancel(self):
36+
config = ConfigFile()
37+
config.create_file(
38+
self.entries["name"].get(),
39+
self.entries["broker"].get(),
40+
self.entries["port"].get(),
41+
self.entries["username"].get(),
42+
self.entries["password"].get()
43+
)
44+
self.main_window.refresh_broker_list()
8345
self.destroy()

new_topic.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,35 @@
1-
"""MQTT Client GUI
2-
1+
"""
2+
MQTT Client GUI - New Topic Creation Window
33
Author: Sahin MERSIN - electrocoder <[email protected]>
4-
5-
Source Code: https://github.com/electrocoder/MQTTClient
6-
7-
MQTT Examples: https://github.com/meseiot/iot-examples
8-
9-
Date: 26.11.2022
10-
11-
File: This script is Create New Topic
124
"""
135

146
import tkinter as tk
15-
7+
from tkinter import ttk
168
from config_file import ConfigFile
179

1810

1911
class NewTopic(tk.Toplevel):
20-
def __init__(self, main_window, font_size):
12+
def __init__(self, main_window):
2113
super().__init__(main_window)
22-
2314
self.main_window = main_window
24-
self.title("MQTT Client Create New Topic")
25-
26-
row = 0
27-
column = 0
15+
self.title("Add New Topic")
16+
self.geometry("300x150")
17+
self.transient(main_window)
18+
self.grab_set()
2819

29-
self.label_topic = tk.Label(self, text="Add Topic",
30-
font=font_size)
31-
self.label_topic.grid(row=row, column=column)
32-
column += 1
33-
self.entry_topic = tk.Entry(self)
34-
self.entry_topic.config(font=font_size)
35-
self.entry_topic.grid(row=row, column=column)
20+
font = ("Helvetica", 12)
21+
frame = ttk.Frame(self, padding="10")
22+
frame.pack(fill="both", expand=True)
3623

37-
row += 1
38-
column = 0
24+
ttk.Label(frame, text="Topic:").grid(row=0, column=0, padx=5, pady=5, sticky="e")
25+
self.entry_topic = ttk.Entry(frame, font=font)
26+
self.entry_topic.grid(row=0, column=1, padx=5, pady=5, sticky="ew")
3927

40-
self.button_cancel = tk.Button(self, text="Cancel",
41-
font=font_size,
42-
command=self.cancel)
43-
self.button_cancel.grid(row=row, column=column, padx=55, pady=55)
44-
column += 1
45-
self.button_save = tk.Button(self, text="Save",
46-
font=font_size,
47-
command=self.save_topic)
48-
self.button_save.grid(row=row, column=column, padx=55, pady=55)
28+
ttk.Button(frame, text="Save", command=self.save_topic).grid(row=1, column=1, pady=10, sticky="e")
29+
ttk.Button(frame, text="Cancel", command=self.destroy).grid(row=1, column=0, pady=10, sticky="w")
4930

5031
def save_topic(self):
51-
topic = ConfigFile().create_topic(self.main_window.entry_broker_text.get(),
52-
self.entry_topic.get())
32+
topic = ConfigFile().create_topic(self.main_window.entry_broker_text.get(), self.entry_topic.get())
33+
self.main_window.refresh_subscribe_list()
5334
self.main_window.entry_subscribe_topic_text.set(topic)
5435
self.destroy()
55-
56-
def cancel(self):
57-
self.destroy()

0 commit comments

Comments
 (0)