def __init__(self, master): self.master = master # 1: Create a builder self.builder = builder = pygubu.Builder() # 2: Load an ui file ui_path = os.path.join(self.ROOT_DIR, 'exemplul_1_v2.ui') builder.add_from_file(ui_path) # 3: Create the widget using a master as parent self.stack_app_window = builder.get_object('stack_app_window', master) self.stack_A = self.builder.get_object('stack_A') self.stack_B = self.builder.get_object('stack_B') self.result = self.builder.get_object('result') self.regenerate_A_btn = self.builder.get_object('regenerate_A_btn') self.regenerate_B_btn = self.builder.get_object('regenerate_B_btn') self.compute_btn = self.builder.get_object('compute_btn') self.regenerate_A_btn['command'] = partial(self.send_request, request='regenerate_A') self.regenerate_B_btn['command'] = partial(self.send_request, request='regenerate_B') self.compute_btn['command'] = partial(self.send_request, request='compute') builder.connect_callbacks(self) self.rabbit_mq = RabbitMq(self)
def __init__(self): super(LibraryApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_2.ui') loadUi(ui_path, self) self.search_btn.clicked.connect(self.search) self.save_as_file_btn.clicked.connect(self.save_as_file) self.rabbit_mq = RabbitMq(self)
def __init__(self): super(StackApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_1_v1.ui') loadUi(ui_path, self) self.regenerate_multimi.clicked.connect( partial(self.send_request, request='regenerate_A')) self.compute_btn.clicked.connect( partial(self.send_request, request='compute')) self.rabbit_mq = RabbitMq(self)
class StackApp(QWidget): ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) A = None B = None def __init__(self): super(StackApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_1_v1.ui') loadUi(ui_path, self) self.regenerate_multimi.clicked.connect( partial(self.send_request, request='regenerate_A')) self.compute_btn.clicked.connect( partial(self.send_request, request='compute')) self.rabbit_mq = RabbitMq(self) def set_response(self, variable, response): if variable == 'A': self.regenerate_A(response) elif variable == 'B': self.regenerate_B(response) elif variable == 'compute': self.compute(response) def send_request(self, request): self.rabbit_mq.send_message(message=request) self.rabbit_mq.receive_message() def regenerate_A(self, response): self.A = response current_result = self.result.toPlainText().split('\n') current_result[0] = 'A: ' + self.A self.stack_A.setText(self.A) self.result.setText('\n'.join(current_result)) def regenerate_B(self, response): self.B = response current_result = self.result.toPlainText().split('\n') if len(current_result) == 1: current_result.append('B: ' + self.B) else: current_result[1] = 'B: ' + self.B self.stack_B.setText(self.B) self.result.setText('\n'.join(current_result)) def regenerate_multimi(self, response): responses = response.split("||") self.regenerate_A(responses[0]) self.regenerate_B(responses[1]) def compute(self, response): dict_response = json.loads(response) result = '' for key in dict_response: result += '{}: {}\n'.format(key, dict_response[key]) self.stack_A.setText(dict_response['A']) self.stack_B.setText(dict_response['B']) self.result.setText(result)
def __init__(self, obj=None, *args, **kwargs): super(StackApp, self).__init__(*args, **kwargs) self.setupUi(self) self.regenerate_A_btn.clicked.connect( partial(self.send_request, request='regenerate_A')) self.regenerate_B_btn.clicked.connect( partial(self.send_request, request='regenerate_B')) self.compute_btn.clicked.connect( partial(self.send_request, request='compute')) self.rabbit_mq = RabbitMq(self)
def __init__(self): super(LibraryApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_2.ui') loadUi(ui_path, self) self.form = Form() self.form.closeEvent = self.CloseEvent self.search_btn.clicked.connect(self.search) self.save_as_file_btn.clicked.connect(self.save_as_file) self.rabbit_mq = RabbitMq(self) self.add_book_btn.clicked.connect(lambda: self.form.exec())
class StackApp(QtWidgets.QWidget, Ui_stack_app_window): ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) A = None B = None def __init__(self, obj=None, *args, **kwargs): super(StackApp, self).__init__(*args, **kwargs) self.setupUi(self) self.regenerate_A_btn.clicked.connect( partial(self.send_request, request='regenerate_A')) self.regenerate_B_btn.clicked.connect( partial(self.send_request, request='regenerate_B')) self.compute_btn.clicked.connect( partial(self.send_request, request='compute')) self.rabbit_mq = RabbitMq(self) def set_response(self, variable, response): if variable == 'A': self.regenerate_A(response) elif variable == 'B': self.regenerate_B(response) elif variable == 'compute': self.compute(response) def send_request(self, request): self.rabbit_mq.send_message(message=request) self.rabbit_mq.receive_message() def regenerate_A(self, response): self.A = response current_result = self.result.toPlainText().split('\n') current_result[0] = 'A: ' + self.A self.stack_A.setText(self.A) self.result.setText('\n'.join(current_result)) def regenerate_B(self, response): self.B = response current_result = self.result.toPlainText().split('\n') if len(current_result) == 1: current_result.append('B: ' + self.B) else: current_result[1] = 'B: ' + self.B self.stack_B.setText(self.B) self.result.setText('\n'.join(current_result)) def compute(self, response): dict_response = json.loads(response) result = '' for key in dict_response: result += '{}: {}\n'.format(key, dict_response[key]) self.stack_A.setText(dict_response['A']) self.stack_B.setText(dict_response['B']) self.result.setText(result)
class App(QWidget): ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) def __init__(self, ui_file): super(App, self).__init__() ui_path = os.path.join(self.ROOT_DIR, ui_file) loadUi(ui_path, self) self.rabbit_mq = RabbitMq(self) def send_request(self, request): print('Sending') self.rabbit_mq.send_message(message=request) self.rabbit_mq.receive_message()
def __init__(self): super(LibraryApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_2.ui') loadUi(ui_path, self) self.search_btn.clicked.connect(self.search) self.save_as_file_btn.clicked.connect(self.save_as_file) self.add_btn.clicked.connect(self.showNewBookWin) self.rabbit_mq = RabbitMq(self) self.widgetNewBook = QWidget() ui_path = os.path.join(self.ROOT_DIR, 'window_add_book.ui') loadUi(ui_path, self.widgetNewBook) self.widgetNewBook.confirm_btn.clicked.connect(self.addBook)
def __init__(self, gui): self.gui = gui self.gui.title('Exemplul 1 cu Tkinter') self.gui.geometry("1050x300") self.stack_A_lbl = tk.Label(master=self.gui, text="Multimea A:") self.stack_B_lbl = tk.Label(master=self.gui, text="Multimea B:") self.stack_A = tk.Label(master=self.gui, text="[1, 2, 3]") self.stack_B = tk.Label(master=self.gui, text="[4, 5, 6]") self.regenerate_A_btn = tk.Button(master=self.gui, text="Generare multimea A", command=partial( self.send_request, request='regenerate_A')) self.regenerate_B_btn = tk.Button(master=self.gui, text="Generare multimea B", command=partial( self.send_request, request='regenerate_B')) self.compute_btn = tk.Button(master=self.gui, text="Calculare expresie", command=partial(self.send_request, request='compute')) self.result = tk.Text(self.gui, width=50, height=10) # alignment on the grid self.stack_A_lbl.grid(row=0, column=0) self.stack_B_lbl.grid(row=1, column=0) self.stack_A.grid(row=0, column=1) self.stack_B.grid(row=1, column=1) self.regenerate_A_btn.grid(row=0, column=2) self.regenerate_B_btn.grid(row=1, column=2) self.compute_btn.grid(row=2, column=2) self.result.grid(row=2, column=0) self.rabbit_mq = RabbitMq(self) self.gui.mainloop()
class LibraryApp(QWidget): ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) def __init__(self): super(LibraryApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_2.ui') loadUi(ui_path, self) self.search_btn.clicked.connect(self.search) self.save_as_file_btn.clicked.connect(self.save_as_file) self.rabbit_mq = RabbitMq(self) self.new_btn.clicked.connect(self.on_click) self.form = None @pyqtSlot() def on_click(self): self.form = Form(self) self.form.show() def set_response(self, response): self.result.setText(response) def send_request(self, request): self.rabbit_mq.send_message(message=request) self.rabbit_mq.receive_message() def search(self): search_string = self.search_bar.text() request = None if not search_string: if self.json_rb.isChecked(): request = 'print:json' elif self.html_rb.isChecked(): request = 'print:html' elif self.xml_rb.isChecked(): print("merge") request = 'print:xml' else: request = 'print:raw' else: if self.author_rb.isChecked(): request = 'find:author={}'.format(search_string) elif self.title_rb.isChecked(): request = 'find:title={}'.format(search_string) else: request = 'fin"html" -> libraryPrinter.printHTML(libraryDAO.getBooks())d:publisher={}'.format( search_string) if self.json_rb.isChecked(): request += ',print:json' elif self.html_rb.isChecked(): request += ',print:html' elif self.xml_rb.isChecked(): print("merge") request += ',print:xml' else: request += ',print:raw' self.send_request(request) def save_as_file(self): options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog file_type = "" if self.json_rb.isChecked(): file_type += 'JSON (*.json)' elif self.html_rb.isChecked(): file_type += 'HTML (*.html)' elif self.xml_rb.isChecked(): file_type += 'XML (*.xml)' else: file_type += 'Text (*.txt)' file_path = str( QFileDialog.getSaveFileName(self, 'Salvare fisier', options=options, filter=file_type)) if file_path: file_path = file_path.split("'")[1] file_path += file_type.split("*")[1][0:-1] try: with open(file_path, 'w') as fp: if file_path.endswith(".html"): fp.write(self.result.toHtml()) else: fp.write(self.result.toPlainText()) except Exception as e: print(e) QMessageBox.warning(self, 'Exemplul 2', 'Nu s-a putut salva fisierul') def add_book(self, autor, titlu, editura, content): request = "" if len(autor): request += "autor:{}".format(autor) else: request += "autor:null" if len(titlu): request += ",titlu:{}".format(titlu) else: request += ",titlu:null" if len(editura): request += ",editura:{}".format(editura) else: request += ",editura:null" if len(content): request += ",continut:{}".format(content) else: request += ",titlu:null" self.send_request(request)
class LibraryApp(QWidget): ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) def __init__(self): super(LibraryApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_2.ui') loadUi(ui_path, self) self.form = Form() self.form.closeEvent = self.CloseEvent self.search_btn.clicked.connect(self.search) self.save_as_file_btn.clicked.connect(self.save_as_file) self.rabbit_mq = RabbitMq(self) self.add_book_btn.clicked.connect(lambda: self.form.exec()) def CloseEvent(self, event): book_info = self.form.get_data() request = 'add:title={}:author={}:publisher={}:content={}'.format( book_info['title'], book_info['author'], book_info['publisher'], book_info['content'] ) print(request) self.send_request(request) def set_response(self, response): self.result.setText(response) def send_request(self, request): self.rabbit_mq.send_message(message=request) self.rabbit_mq.receive_message() def search(self): search_string = self.search_bar.text() request = None if self.json_rb.isChecked(): request = 'print:json' elif self.html_rb.isChecked(): request = 'print:html' elif self.xml_rb.isChecked(): request = 'print:xml' else: request = 'print:raw' if self.author_rb.isChecked(): request += (" " + 'find:author={}'.format(search_string)) elif self.title_rb.isChecked(): request += (" " + 'find:title={}'.format(search_string)) else: request += (" " + 'find:publisher={}'.format(search_string)) self.send_request(request) def save_as_file(self): options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog file_path = str( QFileDialog.getSaveFileName(self, 'Salvare fisier', options=options)) if file_path: file_path = file_path.split("'")[1] if not file_path.endswith('.json') and not file_path.endswith( '.html') and not file_path.endswith('.txt') and not file_path.endswith('.xml'): if self.json_rb.isChecked(): file_path += '.json' elif self.html_rb.isChecked(): file_path += '.html' elif self.xml_rb.isChecked(): file_path += '.xml' else: file_path += '.txt' try: with open(file_path, 'w') as fp: if file_path.endswith(".html"): fp.write(self.result.toHtml()) if file_path.endswith(".json"): fp.write(self.result.toHtml()) if file_path.endswith(".xml"): fp.write(self.result.toPlainText()) else: fp.write(self.result.toPlainText()) except Exception as e: print(e) QMessageBox.warning(self, 'Exemplul 2', 'Nu s-a putut salva fisierul')
class StackApp: ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) A = None B = None def __init__(self, gui): self.gui = gui self.gui.title('Exemplul 1 cu Tkinter') self.gui.geometry("1050x300") self.stack_A_lbl = tk.Label(master=self.gui, text="Multimea A:") self.stack_B_lbl = tk.Label(master=self.gui, text="Multimea B:") self.stack_A = tk.Label(master=self.gui, text="[1, 2, 3]") self.stack_B = tk.Label(master=self.gui, text="[4, 5, 6]") self.regenerate_A_btn = tk.Button(master=self.gui, text="Generare multimea A", command=partial( self.send_request, request='regenerate_A')) self.regenerate_B_btn = tk.Button(master=self.gui, text="Generare multimea B", command=partial( self.send_request, request='regenerate_B')) self.compute_btn = tk.Button(master=self.gui, text="Calculare expresie", command=partial(self.send_request, request='compute')) self.result = tk.Text(self.gui, width=50, height=10) # alignment on the grid self.stack_A_lbl.grid(row=0, column=0) self.stack_B_lbl.grid(row=1, column=0) self.stack_A.grid(row=0, column=1) self.stack_B.grid(row=1, column=1) self.regenerate_A_btn.grid(row=0, column=2) self.regenerate_B_btn.grid(row=1, column=2) self.compute_btn.grid(row=2, column=2) self.result.grid(row=2, column=0) self.rabbit_mq = RabbitMq(self) self.gui.mainloop() def set_response(self, variable, response): if variable == 'A': self.regenerate_A(response) elif variable == 'B': self.regenerate_B(response) elif variable == 'compute': self.compute(response) def send_request(self, request): self.rabbit_mq.send_message(message=request) self.rabbit_mq.receive_message() def regenerate_A(self, response): self.A = response current_result = self.result.get("1.0", tk.END).split('\n') current_result[0] = 'A: ' + self.A self.stack_A['text'] = self.A self.result.delete("1.0", tk.END) self.result.insert(tk.END, '\n'.join(current_result)) def regenerate_B(self, response): self.B = response current_result = self.result.get("1.0", tk.END).split('\n') if len(current_result) == 1: current_result.append('B: ' + self.B) else: current_result[1] = 'B: ' + self.B self.stack_B['text'] = self.B self.result.delete("1.0", tk.END) self.result.insert(tk.END, '\n'.join(current_result)) def compute(self, response): dict_response = json.loads(response) result = '' for key in dict_response: result += '{}: {}\n'.format(key, dict_response[key]) self.stack_A['text'] = dict_response['A'] self.stack_B['text'] = dict_response['B'] self.result.delete("1.0", tk.END) self.result.insert(tk.END, result)
def __init__(self, ui_file): super(App, self).__init__() ui_path = os.path.join(self.ROOT_DIR, ui_file) loadUi(ui_path, self) self.rabbit_mq = RabbitMq(self)
class StackApp: ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) A = None B = None def __init__(self, master): self.master = master # 1: Create a builder self.builder = builder = pygubu.Builder() # 2: Load an ui file ui_path = os.path.join(self.ROOT_DIR, 'exemplul_1_v2.ui') builder.add_from_file(ui_path) # 3: Create the widget using a master as parent self.stack_app_window = builder.get_object('stack_app_window', master) self.stack_A = self.builder.get_object('stack_A') self.stack_B = self.builder.get_object('stack_B') self.result = self.builder.get_object('result') self.regenerate_A_btn = self.builder.get_object('regenerate_A_btn') self.regenerate_B_btn = self.builder.get_object('regenerate_B_btn') self.compute_btn = self.builder.get_object('compute_btn') self.regenerate_A_btn['command'] = partial(self.send_request, request='regenerate_A') self.regenerate_B_btn['command'] = partial(self.send_request, request='regenerate_B') self.compute_btn['command'] = partial(self.send_request, request='compute') builder.connect_callbacks(self) self.rabbit_mq = RabbitMq(self) def set_response(self, variable, response): if variable == 'A': self.regenerate_A(response) elif variable == 'B': self.regenerate_B(response) elif variable == 'compute': self.compute(response) def send_request(self, request): self.rabbit_mq.send_message(message=request) self.rabbit_mq.receive_message() def regenerate_A(self, response): self.A = response current_result = self.result.get("1.0", tk.END).split('\n') current_result[0] = 'A: ' + self.A self.stack_A['text'] = self.A self.result.delete("1.0", tk.END) self.result.insert(tk.END, '\n'.join(current_result)) def regenerate_B(self, response): self.B = response current_result = self.result.get("1.0", tk.END).split('\n') if len(current_result) == 1: current_result.append('B: ' + self.B) else: current_result[1] = 'B: ' + self.B self.stack_B['text'] = self.B self.result.delete("1.0", tk.END) self.result.insert(tk.END, '\n'.join(current_result)) def compute(self, response): dict_response = json.loads(response) result = '' for key in dict_response: result += '{}: {}\n'.format(key, dict_response[key]) self.stack_A['text'] = dict_response['A'] self.stack_B['text'] = dict_response['B'] self.result.delete("1.0", tk.END) self.result.insert(tk.END, result)
class LibraryApp(QWidget): ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) def __init__(self): super(LibraryApp, self).__init__() ui_path = os.path.join(self.ROOT_DIR, 'exemplul_2.ui') loadUi(ui_path, self) self.search_btn.clicked.connect(self.search) self.save_as_file_btn.clicked.connect(self.save_as_file) self.add_btn.clicked.connect(self.showNewBookWin) self.rabbit_mq = RabbitMq(self) self.widgetNewBook = QWidget() ui_path = os.path.join(self.ROOT_DIR, 'window_add_book.ui') loadUi(ui_path, self.widgetNewBook) self.widgetNewBook.confirm_btn.clicked.connect(self.addBook) def set_response(self, response): self.result.setText(response) def send_request(self, request): self.rabbit_mq.send_message(message=request) print("mesaj trimis") self.rabbit_mq.receive_message() print("mesaj primit") def search(self): search_string = self.search_bar.text() request = None if self.json_rb.isChecked(): request = 'print::json' elif self.html_rb.isChecked(): request = 'print::html' elif self.xml_rb.isChecked(): request = 'print::xml' else: request = 'print::raw' if not search_string: request += '&&find::all' else: if self.author_rb.isChecked(): request += '&&find::author={}'.format(search_string) elif self.title_rb.isChecked(): request += '&&find::title={}'.format(search_string) else: request += '&&find::publisher={}'.format(search_string) self.send_request(request) def save_as_file(self): options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog file_path = str( QFileDialog.getSaveFileName(self, 'Salvare fisier', options=options)) if file_path: file_path = file_path.split("'")[1] if not file_path.endswith('.json') and not file_path.endswith('.html') and not file_path.endswith('.txt') \ and not file_path.endswith('.xml'): if self.json_rb.isChecked(): file_path += '.json' elif self.html_rb.isChecked(): file_path += '.html' elif self.xml_rb.isChecked(): file_path += '.xml' else: file_path += '.txt' try: with open(file_path, 'w') as fp: if file_path.endswith('.html'): fp.write(self.result.toHtml()) elif file_path.endswith('.json'): fp.write(self.toJson()) elif file_path.endswith('.xml'): fp.write(self.toXml()) else: fp.write(self.result.toPlainText()) except Exception as e: print(e) QMessageBox.warning(self, 'LibraryApp', 'Nu s-a putut salva fisierul') def toJson(self): plainResult = self.result.toPlainText() elements = ['"Titlu": ', '"Autor": ', '"Editura": ', '"Text": '] jsonResult = list('[\n') firstFlag = True position = -1 index = 0 while position < len(plainResult): prevPosition = position + 1 position = plainResult.find('\n', prevPosition) if -1 == position: break if not plainResult[prevPosition:position]: continue if 0 == index: if firstFlag: jsonResult.append('\t{') firstFlag = False else: jsonResult.append(',\n\t{') jsonResult.append(elements[index]) jsonResult.append('"') jsonResult.append(plainResult[prevPosition:position]) if len(elements) - 1 == index: jsonResult.append('"}') else: jsonResult.append('", ') index = (index + 1) % len(elements) jsonResult.append('\n]\n') return ''.join(jsonResult) def toXml(self): plainResult = self.result.toPlainText() elements = ['\t\t<titlu>', '\t\t<autor>', '\t\t<editura>', '\t\t<text>', '</titlu>\n', '</autor>\n', '</editura>\n', '</text>\n'] jsonResult = list('<carti>') position = -1 index = 0 while position < len(plainResult): prevPosition = position + 1 position = plainResult.find('\n', prevPosition) if -1 == position: break if not plainResult[prevPosition:position]: continue if 0 == index: jsonResult.append('\n\t<carte>\n') jsonResult.append(elements[index]) jsonResult.append(plainResult[prevPosition:position]) jsonResult.append(elements[index + len(elements) // 2]) if (len(elements) // 2 - 1) == index: jsonResult.append('\t</carte>') index = 0 else: index = index + 1 jsonResult.append('\n</carti>\n') return ''.join(jsonResult) def showNewBookWin(self): self.widgetNewBook.show() def addBook(self): author = self.widgetNewBook.textEditAutor.toPlainText().strip() title = self.widgetNewBook.textEditTitlu.toPlainText().strip() publisher = self.widgetNewBook.textEditEditura.toPlainText().strip() text = self.widgetNewBook.textEditText.toPlainText().strip() if not author or not title or not publisher or not text: popupWin = QMessageBox() popupWin.setWindowTitle('Atentie') popupWin.setText('Toate campurile sunt obligatorii!') popupWin.exec() return request = 'add::author={};;title={};;publisher={};;text={}'.format(author, title, publisher, text) self.send_request(request) self.widgetNewBook.close()