|
18 | 18 |
|
19 | 19 | import paho.mqtt.client as mqtt |
20 | 20 |
|
21 | | -import about as about |
22 | | -import new_connect as new_connect |
23 | | -import open_connect as open_connect |
| 21 | +import new_connect |
| 22 | +import new_topic |
24 | 23 | import subscriber |
| 24 | +import about |
| 25 | + |
25 | 26 | from config_file import ConfigFile |
26 | | -from main_window_frame_ui import MainWindowFrameUI |
27 | 27 |
|
28 | 28 |
|
29 | 29 | class App(tk.Tk): |
30 | 30 | def __init__(self): |
31 | 31 | super().__init__() |
32 | 32 |
|
33 | | - self.title('MQTT Client') |
34 | | - self.geometry('700x350') |
| 33 | + self.title('MQTT Client 0v3') |
| 34 | + self.geometry('850x450') |
| 35 | + |
| 36 | + font_size = 14 |
| 37 | + self.text_font = Font(size=font_size) |
| 38 | + |
| 39 | + self.msg_filter = False |
| 40 | + row = 0 |
| 41 | + column = 0 |
| 42 | + |
| 43 | + self.label_broker = tk.Label(self, text="Broker", font=font_size) |
| 44 | + self.label_broker.grid(row=row, column=column, sticky=tk.W) |
| 45 | + column += 1 |
| 46 | + |
| 47 | + self.entry_broker_text = tk.StringVar(self) |
| 48 | + self.options_list = ConfigFile().read_sections() |
| 49 | + self.entry_broker_text.set(self.options_list[0]) |
| 50 | + self.entry_broker = tk.OptionMenu(self, |
| 51 | + self.entry_broker_text, |
| 52 | + *self.options_list) |
| 53 | + |
| 54 | + self.entry_broker.grid(row=row, column=column) |
| 55 | + |
| 56 | + column += 1 |
| 57 | + |
| 58 | + self.button_connect = tk.Button(self, |
| 59 | + text="Connect", font=font_size, |
| 60 | + command=self.button_connect) |
| 61 | + self.button_connect.grid(row=row, column=column) |
| 62 | + |
| 63 | + column += 1 |
| 64 | + |
| 65 | + self.button_disconnect = tk.Button(self, |
| 66 | + text="Disconnect", font=font_size, |
| 67 | + state=tk.DISABLED, |
| 68 | + command=self.button_disconnect) |
| 69 | + self.button_disconnect.grid(row=row, column=column, sticky=tk.W) |
| 70 | + |
| 71 | + row += 1 |
| 72 | + column = 0 |
| 73 | + |
| 74 | + # publich topic |
| 75 | + self.label_publich_topic = tk.Label(self, text="Publish Topic", |
| 76 | + font=font_size) |
| 77 | + self.label_publich_topic.grid(row=row, column=column, sticky=tk.W) |
| 78 | + |
| 79 | + column += 1 |
| 80 | + self.entry_publich_topic_text = tk.StringVar(self) |
| 81 | + self.entry_publich_topic = tk.Entry(self, |
| 82 | + textvariable=self.entry_publich_topic_text, |
| 83 | + font=font_size) |
| 84 | + self.entry_publich_topic.grid(row=row, column=column) |
| 85 | + |
| 86 | + column += 1 |
| 87 | + self.label_publich_msg_topic = tk.Label(self, text="Publish Message", |
| 88 | + font=font_size) |
| 89 | + self.label_publich_msg_topic.grid(row=row, column=column) |
| 90 | + |
| 91 | + column += 1 |
| 92 | + self.entry_publich_topic_msg_text = tk.StringVar(self) |
| 93 | + self.entry_publich_msg_topic = tk.Entry(self, |
| 94 | + textvariable=self.entry_publich_topic_msg_text, |
| 95 | + font=font_size) |
| 96 | + self.entry_publich_msg_topic.grid(row=row, column=column) |
| 97 | + |
| 98 | + column += 1 |
| 99 | + self.button_publich_topic = tk.Button(self, |
| 100 | + text="Publish", font=font_size, |
| 101 | + command=self.button_publish_topic) |
| 102 | + self.button_publich_topic.grid(row=row, column=column) |
| 103 | + |
| 104 | + row += 1 |
| 105 | + column = 0 |
| 106 | + |
| 107 | + # subscribe topic |
| 108 | + self.label_subscribe_topic = tk.Label(self, text="Subscribe Topic", |
| 109 | + font=font_size) |
| 110 | + self.label_subscribe_topic.grid(row=row, column=column, sticky=tk.W) |
35 | 111 |
|
36 | | - self.text_font = Font(size=12) |
| 112 | + column += 1 |
| 113 | + self.entry_subscribe_topic_text = tk.StringVar(self) |
| 114 | + self.options_list = ["-", ] |
| 115 | + self.entry_subscribe_topic = tk.OptionMenu(self, |
| 116 | + self.entry_subscribe_topic_text, |
| 117 | + *self.options_list, command=self.add_subscribe_topic) |
| 118 | + self.entry_subscribe_topic.config(font=font_size) |
| 119 | + menu = self.nametowidget( |
| 120 | + self.entry_subscribe_topic.menuname) |
| 121 | + menu.config(font=font_size) |
37 | 122 |
|
38 | | - self.main_window_frame = tk.Frame() |
39 | | - self.main_window_frame.pack() |
| 123 | + self.entry_subscribe_topic.grid(row=row, column=column) |
40 | 124 |
|
41 | | - self.main_window_frame_ui = MainWindowFrameUI(self.main_window_frame, |
42 | | - self, |
43 | | - font_size=self.text_font) |
44 | | - self.main_window_frame_ui.pack() |
| 125 | + column += 1 |
| 126 | + self.button_subscribe_topic = tk.Button(self, |
| 127 | + text="Subscribe", |
| 128 | + font=font_size, |
| 129 | + state=tk.DISABLED, |
| 130 | + command=self.button_subscribe) |
| 131 | + self.button_subscribe_topic.grid(row=row, column=column) |
45 | 132 |
|
46 | | - self.subscriber = subscriber.Subscriber(self.main_window_frame_ui, |
| 133 | + column += 1 |
| 134 | + self.button_add_subscribe_topic = tk.Button(self, |
| 135 | + text="Add Subscribe Topic", |
| 136 | + font=font_size, |
| 137 | + state=tk.DISABLED, |
| 138 | + command=self.add_subscribe_topic) |
| 139 | + self.button_add_subscribe_topic.grid(row=row, column=column) |
| 140 | + |
| 141 | + # filter msg |
| 142 | + row += 1 |
| 143 | + column = 0 |
| 144 | + |
| 145 | + self.label_msg_filter = tk.Label(self, text="Filter Message", |
| 146 | + font=font_size) |
| 147 | + self.label_msg_filter.grid(row=row, column=column, sticky=tk.W) |
| 148 | + self.label_msg_filter.grid(row=row, column=column, sticky=tk.W) |
| 149 | + |
| 150 | + column += 1 |
| 151 | + |
| 152 | + self.entry_msg_filter_text = tk.StringVar(self) |
| 153 | + self.entry_msg_filter = tk.Entry(self, |
| 154 | + textvariable=self.entry_msg_filter_text, |
| 155 | + font=font_size) |
| 156 | + self.entry_msg_filter.grid(row=row, column=column) |
| 157 | + column += 1 |
| 158 | + |
| 159 | + self.button_filter_add = tk.Button(self, |
| 160 | + text="Add Filter", font=font_size, |
| 161 | + state=tk.DISABLED, |
| 162 | + command=self.add_filter) |
| 163 | + self.button_filter_add.grid(row=row, column=column) |
| 164 | + column += 1 |
| 165 | + |
| 166 | + self.button_filter_remove = tk.Button(self, |
| 167 | + text="Remove Filter", |
| 168 | + font=font_size, |
| 169 | + state=tk.DISABLED, |
| 170 | + command=self.remove_filter) |
| 171 | + self.button_filter_remove.grid(row=row, column=column) |
| 172 | + |
| 173 | + row += 1 |
| 174 | + column = 0 |
| 175 | + |
| 176 | + # subscribe list |
| 177 | + self.listbox_message = tk.Text(self, font=font_size, height=12) |
| 178 | + self.listbox_message.grid(row=row, column=column, columnspan=6, |
| 179 | + ipadx=11, ipady=11, padx=22, pady=22) |
| 180 | + |
| 181 | + # menu |
| 182 | + menubar = tk.Menu(self) |
| 183 | + self.config(menu=menubar) |
| 184 | + |
| 185 | + menu_connect = tk.Menu(menubar, tearoff=0) |
| 186 | + menu_connect.add_command(label='New Connect', |
| 187 | + command=self.new_connect_window) |
| 188 | + # menu_connect.add_command(label='Open Connect', |
| 189 | + # command=self.open_connect_window) |
| 190 | + menu_connect.add_separator() |
| 191 | + menu_connect.add_command(label='Exit', command=self.quit) |
| 192 | + menubar.add_cascade(label="Connect", menu=menu_connect) |
| 193 | + |
| 194 | + menu_help = tk.Menu(menubar, tearoff=0) |
| 195 | + menu_help.add_command(label='Help', command=self.help) |
| 196 | + menu_help.add_command(label='About', |
| 197 | + command=self.about_window) |
| 198 | + menubar.add_cascade(label="Help", menu=menu_help) |
| 199 | + |
| 200 | + # status bar |
| 201 | + self.connect_status_text = tk.StringVar() |
| 202 | + self.connect_status_text.set("...") |
| 203 | + self.connect_status = tk.Label(self, |
| 204 | + textvariable=self.connect_status_text, |
| 205 | + relief=tk.SUNKEN, anchor="w") |
| 206 | + row += 1 |
| 207 | + column = 0 |
| 208 | + |
| 209 | + self.connect_status.grid(row=row, column=column) |
| 210 | + |
| 211 | + self.subscriber = subscriber.Subscriber(self, |
47 | 212 | mqtt.Client()) |
48 | 213 |
|
49 | 214 | def button_connect(self): |
50 | 215 | print("button_connect") |
51 | | - if self.main_window_frame_ui.entry_broker_text.get(): |
| 216 | + if self.entry_broker_text.get(): |
52 | 217 | broker, port, username, password = ConfigFile().read_broker( |
53 | | - self.main_window_frame_ui.entry_broker_text.get()) |
| 218 | + self.entry_broker_text.get()) |
54 | 219 | print(broker, port, username, password) |
55 | 220 | if self.subscriber.connect_start(broker, port, username, password): |
56 | | - self.main_window_frame_ui.connect_status_text.set("Connected") |
57 | | - self.main_window_frame_ui.button_connect["state"] = tk.DISABLED |
58 | | - self.main_window_frame_ui.button_connect["text"] = "Connected" |
59 | | - self.main_window_frame_ui.button_disconnect[ |
| 221 | + self.connect_status_text.set("Connected") |
| 222 | + self.button_connect["state"] = tk.DISABLED |
| 223 | + self.button_connect["text"] = "Connected" |
| 224 | + self.button_disconnect[ |
60 | 225 | "state"] = tk.NORMAL |
61 | | - self.main_window_frame_ui.button_subscribe_topic[ |
| 226 | + self.button_subscribe_topic[ |
62 | 227 | "state"] = tk.NORMAL |
63 | | - self.main_window_frame_ui.button_publich_topic[ |
| 228 | + self.button_publich_topic[ |
64 | 229 | "state"] = tk.NORMAL |
65 | | - |
| 230 | + self.subscribe_list(broker) |
| 231 | + self.button_add_subscribe_topic["state"] = tk.NORMAL |
66 | 232 | else: |
67 | 233 | messagebox.showerror("showerror", "Please select broker.") |
68 | 234 |
|
| 235 | + def subscribe_list(self, broker): |
| 236 | + self.entry_subscribe_topic['menu'].delete(0, 'end') |
| 237 | + |
| 238 | + new_choices = ConfigFile().read_topics(broker).split(',') |
| 239 | + for choice in new_choices: |
| 240 | + if choice: |
| 241 | + self.entry_subscribe_topic['menu'].add_command( |
| 242 | + label=choice, |
| 243 | + command=tk._setit(self.entry_subscribe_topic_text, |
| 244 | + choice)) |
| 245 | + self.entry_subscribe_topic_text.set(choice) |
| 246 | + |
69 | 247 | def button_disconnect(self): |
70 | 248 | print("button_disconnect") |
71 | 249 | if self.subscriber.connect_stop(): |
72 | | - self.main_window_frame_ui.connect_status_text.set("Disconnect") |
73 | | - self.main_window_frame_ui.button_connect["state"] = tk.NORMAL |
74 | | - self.main_window_frame_ui.button_connect["text"] = "Connect" |
75 | | - self.main_window_frame_ui.button_disconnect["state"] = tk.DISABLED |
76 | | - self.main_window_frame_ui.button_subscribe_topic[ |
| 250 | + self.connect_status_text.set("Disconnect") |
| 251 | + self.button_connect["state"] = tk.NORMAL |
| 252 | + self.button_connect["text"] = "Connect" |
| 253 | + self.button_disconnect["state"] = tk.DISABLED |
| 254 | + self.button_subscribe_topic[ |
77 | 255 | "state"] = tk.DISABLED |
78 | | - self.main_window_frame_ui.button_publich_topic[ |
| 256 | + self.button_publich_topic[ |
79 | 257 | "state"] = tk.DISABLED |
| 258 | + self.button_add_subscribe_topic["state"] = tk.DISABLED |
80 | 259 |
|
81 | | - def button_subscribe_topic(self): |
82 | | - print("button_subscribe_topic") |
| 260 | + def button_subscribe(self): |
| 261 | + print("button_subscribe") |
83 | 262 | self.subscriber.subscribe_start( |
84 | | - self.main_window_frame_ui.entry_subscribe_topic_text.get()) |
85 | | - self.main_window_frame_ui.button_filter_add[ |
| 263 | + self.entry_subscribe_topic_text.get()) |
| 264 | + self.button_filter_add[ |
86 | 265 | "state"] = tk.NORMAL |
87 | | - self.main_window_frame_ui.button_filter_remove[ |
| 266 | + self.button_filter_remove[ |
88 | 267 | "state"] = tk.DISABLED |
89 | 268 |
|
90 | 269 | def button_publish_topic(self): |
91 | 270 | print("button_publish_topic") |
92 | | - topic = self.main_window_frame_ui.entry_publich_topic_text.get() |
93 | | - msg = self.main_window_frame_ui.entry_publich_topic_msg_text.get() |
94 | | - self.main_window_frame_ui.listbox_message.insert(tk.END, |
95 | | - "> {}".format(msg)) |
96 | | - self.main_window_frame_ui.listbox_message.see("end") |
| 271 | + topic = self.entry_publich_topic_text.get() |
| 272 | + msg = self.entry_publich_topic_msg_text.get() |
| 273 | + self.listbox_message.insert(tk.END, |
| 274 | + "> {}".format(msg)) |
| 275 | + self.listbox_message.see("end") |
97 | 276 | self.subscriber.publish_start(topic, msg) |
98 | 277 |
|
99 | 278 | def about_window(self): |
100 | | - about.AboutWindow(self.master, self.text_font) |
| 279 | + about.AboutWindow(self, self.text_font) |
101 | 280 |
|
102 | 281 | def help(self): |
103 | 282 | webbrowser.open_new_tab("https://github.com/electrocoder/MQTTClient") |
104 | 283 |
|
105 | 284 | def new_connect_window(self): |
106 | | - new_connect.NewConnect(self.master, self.text_font) |
| 285 | + new_connect.NewConnect(self, self.text_font) |
107 | 286 |
|
108 | | - def open_connect_window(self): |
109 | | - open_connect.OpenConnect(self.main_window_frame_ui, self.text_font) |
| 287 | + def add_subscribe_topic(self, *args): |
| 288 | + print("add_subscribe_topic") |
| 289 | + new_topic.NewTopic(self, self.text_font) |
110 | 290 |
|
111 | 291 | def add_filter(self): |
112 | | - self.main_window_frame_ui.msg_filter = True |
113 | | - self.main_window_frame_ui.button_filter_add[ |
| 292 | + self.msg_filter = True |
| 293 | + self.button_filter_add[ |
114 | 294 | "state"] = tk.DISABLED |
115 | | - self.main_window_frame_ui.button_filter_remove[ |
| 295 | + self.button_filter_remove[ |
116 | 296 | "state"] = tk.NORMAL |
117 | 297 |
|
118 | 298 | def remove_filter(self): |
119 | | - self.main_window_frame_ui.msg_filter = False |
120 | | - self.main_window_frame_ui.button_filter_add[ |
| 299 | + self.msg_filter = False |
| 300 | + self.button_filter_add[ |
121 | 301 | "state"] = tk.NORMAL |
122 | | - self.main_window_frame_ui.button_filter_remove[ |
| 302 | + self.button_filter_remove[ |
123 | 303 | "state"] = tk.DISABLED |
124 | 304 |
|
125 | 305 |
|
|
0 commit comments