1616from tkinter import messagebox
1717from tkinter .font import Font
1818
19- import paho .mqtt .client as mqtt
20-
19+ import about
2120import new_connect
2221import new_topic
2322import subscriber
24- import about
25-
2623from config_file import ConfigFile
2724
2825
2926class App (tk .Tk ):
3027 def __init__ (self ):
3128 super ().__init__ ()
3229
33- self .title ('MQTT Client 0v3' )
34- self .geometry ('850x450' )
30+ self .title ('MQTT Client 0v4' )
31+ self .geometry ('750x350' )
32+
33+ # self.rowconfigure(0, weight=1)
34+ # self.columnconfigure(0, weight=1)
35+
36+ ipadding = {'ipadx' : 1 , 'ipady' : 1 }
37+
38+ frame = tk .Frame (self )
39+ frame .pack (fill = tk .BOTH , expand = True )
3540
36- font_size = 14
41+ font_size = 12
3742 self .text_font = Font (size = font_size )
3843
3944 self .msg_filter = False
40- row = 0
41- column = 0
4245
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+ self .label_broker = tk .Label (frame , text = "Broker" , font = font_size )
47+ self .label_broker .pack (** ipadding , side = tk .LEFT )
4648
4749 self .entry_broker_text = tk .StringVar (self )
4850 self .options_list = ConfigFile ().read_sections ()
4951 self .entry_broker_text .set (self .options_list [0 ])
50- self .entry_broker = tk .OptionMenu (self ,
52+ self .entry_broker = tk .OptionMenu (frame ,
5153 self .entry_broker_text ,
5254 * self .options_list )
5355
54- self .entry_broker .grid (row = row , column = column )
56+ self .entry_broker .config (font = font_size )
57+ menu = self .nametowidget (
58+ self .entry_broker .menuname )
59+ menu .config (font = font_size )
5560
56- column += 1
61+ self . entry_broker . pack ( ** ipadding , expand = True , side = tk . LEFT )
5762
58- self .button_connect = tk .Button (self ,
63+ self .button_connect = tk .Button (frame ,
5964 text = "Connect" , font = font_size ,
6065 command = self .button_connect )
61- self .button_connect .grid (row = row , column = column )
62-
63- column += 1
66+ self .button_connect .pack (** ipadding , expand = True , side = tk .LEFT )
6467
65- self .button_disconnect = tk .Button (self ,
68+ self .button_disconnect = tk .Button (frame ,
6669 text = "Disconnect" , font = font_size ,
6770 state = tk .DISABLED ,
6871 command = self .button_disconnect )
69- self .button_disconnect .grid ( row = row , column = column , sticky = tk .W )
72+ self .button_disconnect .pack ( ** ipadding , expand = True , side = tk .LEFT )
7073
71- row += 1
72- column = 0
74+ frame1 = tk . Frame ( self )
75+ frame1 . pack ( fill = tk . BOTH , expand = True )
7376
74- # publich topic
75- self .label_publich_topic = tk .Label (self , text = "Publish Topic" ,
77+ # publish topic
78+ self .label_publish_topic = tk .Label (frame1 , text = "Publish Topic" ,
7679 font = font_size )
77- self .label_publich_topic .grid (row = row , column = column , sticky = tk .W )
80+ self .label_publish_topic .pack (** ipadding ,
81+ side = tk .LEFT )
7882
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+ self .entry_publish_topic_text = tk .StringVar (self )
84+ self .entry_publish_topic = tk .Entry (frame1 ,
85+ textvariable = self .entry_publish_topic_text ,
8386 font = font_size )
84- self .entry_publich_topic .grid (row = row , column = column )
87+ self .entry_publish_topic .pack (** ipadding , expand = True ,
88+ side = tk .LEFT )
8589
86- column += 1
87- self .label_publich_msg_topic = tk .Label (self , text = "Publish Message" ,
90+ self .label_publish_msg_topic = tk .Label (frame1 , text = "Publish Message" ,
8891 font = font_size )
89- self .label_publich_msg_topic .grid (row = row , column = column )
92+ self .label_publish_msg_topic .pack (** ipadding , expand = True ,
93+ side = tk .LEFT )
9094
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+ self .entry_publish_topic_msg_text = tk .StringVar (self )
96+ self .entry_publish_msg_topic = tk .Entry (frame1 ,
97+ textvariable = self .entry_publish_topic_msg_text ,
9598 font = font_size )
96- self .entry_publich_msg_topic .grid (row = row , column = column )
99+ self .entry_publish_msg_topic .pack (** ipadding , expand = True ,
100+ side = tk .LEFT )
97101
98- column += 1
99- self .button_publich_topic = tk .Button (self ,
102+ self .button_publish_topic = tk .Button (frame1 ,
100103 text = "Publish" , font = font_size ,
101104 command = self .button_publish_topic )
102- self .button_publich_topic .grid (row = row , column = column )
105+ self .button_publish_topic .pack (** ipadding , expand = True ,
106+ side = tk .LEFT )
103107
104- row += 1
105- column = 0
108+ frame2 = tk . Frame ( self )
109+ frame2 . pack ( fill = tk . BOTH , expand = True )
106110
107111 # subscribe topic
108- self .label_subscribe_topic = tk .Label (self , text = "Subscribe Topic" ,
112+ self .label_subscribe_topic = tk .Label (frame2 , text = "Subscribe Topic" ,
109113 font = font_size )
110- self .label_subscribe_topic .grid (row = row , column = column , sticky = tk .W )
114+ self .label_subscribe_topic .pack (** ipadding ,
115+ side = tk .LEFT )
111116
112- column += 1
113117 self .entry_subscribe_topic_text = tk .StringVar (self )
114118 self .options_list = ["-" , ]
115- self .entry_subscribe_topic = tk .OptionMenu (self ,
119+ self .entry_subscribe_topic = tk .OptionMenu (frame2 ,
116120 self .entry_subscribe_topic_text ,
117- * self .options_list , command = self .add_subscribe_topic )
121+ * self .options_list ,
122+ command = self .add_subscribe_topic )
118123 self .entry_subscribe_topic .config (font = font_size )
119124 menu = self .nametowidget (
120125 self .entry_subscribe_topic .menuname )
121126 menu .config (font = font_size )
122127
123- self .entry_subscribe_topic .grid (row = row , column = column )
128+ self .entry_subscribe_topic .pack (** ipadding , expand = True ,
129+ side = tk .LEFT )
124130
125- column += 1
126- self .button_subscribe_topic = tk .Button (self ,
131+ self .button_subscribe_topic = tk .Button (frame2 ,
127132 text = "Subscribe" ,
128133 font = font_size ,
129134 state = tk .DISABLED ,
130135 command = self .button_subscribe )
131- self .button_subscribe_topic .grid (row = row , column = column )
136+ self .button_subscribe_topic .pack (** ipadding , expand = True ,
137+ side = tk .LEFT )
132138
133- column += 1
134- self .button_add_subscribe_topic = tk .Button (self ,
139+ self .button_add_subscribe_topic = tk .Button (frame2 ,
135140 text = "Add Subscribe Topic" ,
136141 font = font_size ,
137142 state = tk .DISABLED ,
138143 command = self .add_subscribe_topic )
139- self .button_add_subscribe_topic .grid (row = row , column = column )
144+ self .button_add_subscribe_topic .pack (** ipadding , expand = True ,
145+ side = tk .LEFT )
146+
147+ frame3 = tk .Frame (self )
148+ frame3 .pack (fill = tk .BOTH , expand = True )
140149
141150 # filter msg
142- row += 1
143- column = 0
144151
145- self .label_msg_filter = tk .Label (self , text = "Filter Message" ,
152+ self .label_msg_filter = tk .Label (frame3 , text = "Filter Message" ,
146153 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
154+ self .label_msg_filter .pack (** ipadding ,
155+ side = tk .LEFT )
151156
152157 self .entry_msg_filter_text = tk .StringVar (self )
153- self .entry_msg_filter = tk .Entry (self ,
158+ self .entry_msg_filter = tk .Entry (frame3 ,
154159 textvariable = self .entry_msg_filter_text ,
155160 font = font_size )
156- self .entry_msg_filter .grid ( row = row , column = column )
157- column += 1
161+ self .entry_msg_filter .pack ( ** ipadding , expand = True ,
162+ side = tk . LEFT )
158163
159- self .button_filter_add = tk .Button (self ,
164+ self .button_filter_add = tk .Button (frame3 ,
160165 text = "Add Filter" , font = font_size ,
161166 state = tk .DISABLED ,
162167 command = self .add_filter )
163- self .button_filter_add .grid ( row = row , column = column )
164- column += 1
168+ self .button_filter_add .pack ( ** ipadding , expand = True ,
169+ side = tk . LEFT )
165170
166- self .button_filter_remove = tk .Button (self ,
171+ self .button_filter_remove = tk .Button (frame3 ,
167172 text = "Remove Filter" ,
168173 font = font_size ,
169174 state = tk .DISABLED ,
170175 command = self .remove_filter )
171- self .button_filter_remove .grid (row = row , column = column )
176+ self .button_filter_remove .pack (** ipadding , expand = True ,
177+ side = tk .LEFT )
172178
173- row += 1
174- column = 0
179+ frame4 = tk . Frame ( self )
180+ frame4 . pack ( fill = tk . BOTH , expand = True )
175181
176182 # 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 )
183+ self .listbox_message = tk .Text (frame4 , font = font_size , height = 10 )
184+ self .listbox_message .pack ( ** ipadding , expand = True , fill = tk . BOTH ,
185+ side = tk . LEFT )
180186
181187 # menu
182188 menubar = tk .Menu (self )
@@ -197,45 +203,45 @@ def __init__(self):
197203 command = self .about_window )
198204 menubar .add_cascade (label = "Help" , menu = menu_help )
199205
206+ frame5 = tk .Frame (self )
207+ frame5 .pack (fill = tk .BOTH , expand = True )
208+
200209 # status bar
201210 self .connect_status_text = tk .StringVar ()
202211 self .connect_status_text .set ("..." )
203- self .connect_status = tk .Label (self ,
212+ self .connect_status = tk .Label (frame5 ,
204213 textvariable = self .connect_status_text ,
205- relief = tk .SUNKEN , anchor = "w" )
206- row += 1
207- column = 0
214+ bd = 1 ,
215+ relief = tk .SUNKEN , anchor = tk .W )
208216
209- self .connect_status .grid ( row = row , column = column )
217+ self .connect_status .pack ( ** ipadding , fill = tk . BOTH , expand = True )
210218
211- self .subscriber = subscriber .Subscriber (self ,
212- mqtt .Client ())
219+ self .subscriber = subscriber .Subscriber (self )
213220
214221 def button_connect (self ):
215- print ("button_connect" )
216222 if self .entry_broker_text .get ():
217- broker , port , username , password = ConfigFile ().read_broker (
223+ name , broker , port , username , password = ConfigFile ().read_broker (
218224 self .entry_broker_text .get ())
219- print ( broker , port , username , password )
220- if self . subscriber . connect_start ( broker , port , username , password ):
225+ if self . subscriber . connect_start ( name , broker , port , username ,
226+ password ):
221227 self .connect_status_text .set ("Connected" )
222228 self .button_connect ["state" ] = tk .DISABLED
223229 self .button_connect ["text" ] = "Connected"
224230 self .button_disconnect [
225231 "state" ] = tk .NORMAL
226232 self .button_subscribe_topic [
227233 "state" ] = tk .NORMAL
228- self .button_publich_topic [
234+ self .button_publish_topic [
229235 "state" ] = tk .NORMAL
230- self .subscribe_list (broker )
236+ self .subscribe_list (name )
231237 self .button_add_subscribe_topic ["state" ] = tk .NORMAL
232238 else :
233239 messagebox .showerror ("showerror" , "Please select broker." )
234240
235- def subscribe_list (self , broker ):
241+ def subscribe_list (self , name ):
236242 self .entry_subscribe_topic ['menu' ].delete (0 , 'end' )
237243
238- new_choices = ConfigFile ().read_topics (broker ).split (',' )
244+ new_choices = ConfigFile ().read_topics (name ).split (',' )
239245 for choice in new_choices :
240246 if choice :
241247 self .entry_subscribe_topic ['menu' ].add_command (
@@ -253,7 +259,7 @@ def button_disconnect(self):
253259 self .button_disconnect ["state" ] = tk .DISABLED
254260 self .button_subscribe_topic [
255261 "state" ] = tk .DISABLED
256- self .button_publich_topic [
262+ self .button_publish_topic [
257263 "state" ] = tk .DISABLED
258264 self .button_add_subscribe_topic ["state" ] = tk .DISABLED
259265
@@ -268,8 +274,8 @@ def button_subscribe(self):
268274
269275 def button_publish_topic (self ):
270276 print ("button_publish_topic" )
271- topic = self .entry_publich_topic_text .get ()
272- msg = self .entry_publich_topic_msg_text .get ()
277+ topic = self .entry_publish_topic_text .get ()
278+ msg = self .entry_publish_topic_msg_text .get ()
273279 self .listbox_message .insert (tk .END ,
274280 "> {}" .format (msg ))
275281 self .listbox_message .see ("end" )
0 commit comments