import tkinter as tk import config as conf import pika from tkinter.filedialog import askopenfilename, asksaveasfilename def send_queue_message(): exchange_name = 'simple-editor' message_all = 'Sent from RabbitMQ' print(message_all) credentials= pika.PlainCredentials(username= conf.username, password= conf.password) connection= pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672, credentials= credentials)) channel= connection.channel() channel.exchange_declare(exchange = exchange_name, durable = True, exchange_type = 'topic') channel.queue_declare(queue = 'AllInfo') txt = txt_edit.get("1.0", tk.END) channel.queue_bind(exchange = exchange_name, queue = 'AllInfo', routing_key = 'new') channel.basic_publish(exchange = exchange_name, routing_key = 'new', body = txt) channel.close() def get_queue_message(): exchange_name = 'simple-editor' credentials= pika.PlainCredentials(username= conf.username, password= conf.password) connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672, credentials= credentials)) channel = connection.channel() channel.exchange_declare(exchange_name, durable=True, exchange_type='topic') txt_edit.delete("1.0", tk.END) def callback(ch,method,properties,body): message = 'Message from Queue Part: ' + body.decode("utf-8") txt_edit.insert(tk.END, message) ch.basic_publish('exchange_not_exist', routing_key='new',cbody='Nope this is wrong') ##ch.basic_ack(delivery_tag = method.delivery_tag + 1) # Display the message parts channel.queue_bind(exchange = exchange_name, queue = 'AllInfo', routing_key = 'new') channel.basic_consume(queue='AllInfo', on_message_callback=callback, auto_ack=True) ##channel.consume(queue = 'AllInfo') # Close the channel and the connection channel.close() connection.close() message_all = 'Retrieved from RabbitMQ' print(message_all) def open_file(): """Open a file for editing.""" filepath = askopenfilename( filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")] ) if not filepath: return txt_edit.delete("1.0", tk.END) with open(filepath, mode="r", encoding="utf-8") as input_file: text = input_file.read() txt_edit.insert(tk.END, text) window.title(f"Simple Text Editor - {filepath}") def save_file(): """Save the current file as a new file.""" filepath = asksaveasfilename( defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")], ) if not filepath: return with open(filepath, mode="w", encoding="utf-8") as output_file: text = txt_edit.get("1.0", tk.END) output_file.write(text) window.title(f"Simple Text Editor - {filepath}") window = tk.Tk() window.title("Simple Text Editor") window.rowconfigure(0, minsize=800, weight=1) window.columnconfigure(1, minsize=800, weight=1) txt_edit = tk.Text(window) frm_buttons = tk.Frame(window, relief=tk.RAISED, bd=2) btn_open = tk.Button(frm_buttons, text="Open", command=open_file) btn_save = tk.Button(frm_buttons, text="Save As...", command=save_file) btn_send = tk.Button(frm_buttons, text="Send", command=send_queue_message) btn_receive = tk.Button(frm_buttons, text="Receive", command=get_queue_message) btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5) btn_save.grid(row=1, column=0, sticky="ew", padx=5) btn_send.grid(row=2, column=0, sticky="ew", padx=5, pady=5) btn_receive.grid(row=3, column=0, sticky="ew", padx=5, pady=5) frm_buttons.grid(row=0, column=0, sticky="ns") txt_edit.grid(row=0, column=1, sticky="nsew") window.mainloop()