Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import Tk, Label, Button, Entry, StringVar, W, E, END
- import string
- class Converter:
- def __init__(self, master=Tk):
- self.master = master
- self.master.title = "Converter"
- self.output = ""
- self.cells = {
- 'a': 'A1',
- 'b': 'B1',
- 'c': 'C1',
- 'd': 'D1',
- }
- self.cells_entry = {}
- self.output_text = StringVar()
- self.output_text.set(self.output)
- self.output_label = Label(master, textvariable=self.output_text)
- self.label = Label(master, text="Output:")
- self.entry = Entry(master)
- self.convert_button = Button(
- master, text="Convert", command=lambda: self.convert(self.entry.get(), []))
- self.copy_button = Button(
- master, text="Copy", command=lambda: self.copy_text_to_clipboard(self.output))
- self.add_new_cell = Button(
- master, text="Add new input", command=lambda: self.add_new_cell_function())
- self.label.grid(row=0, column=0, sticky=W)
- self.output_label.grid(row=0, column=1, columnspan=3, sticky=E)
- self.copy_button.grid(row=1, column=0)
- self.entry.grid(row=2, column=0, columnspan=3, sticky=W + E)
- self.convert_button.grid(row=2, column=4)
- self.add_new_cell.grid(row=3, column=0)
- self.draw_cels()
- def convert(self, string, cells):
- print(self.cells["b"])
- for key in self.cells:
- string = string.replace(key, self.cells[key])
- sums = string.split('+')
- converted = ""
- array = []
- for the_sum in sums:
- products = the_sum.split('*')
- if len(products) > 1:
- expression = "AND("
- for product in products:
- if product[0] == '!':
- product = product.replace('!', '')
- product = f"NOT({product})"
- expression += product + ";"
- expression = expression[:-1]
- expression += ")"
- array.append(expression)
- else:
- array.append(the_sum)
- if len(array) > 1:
- converted += "OR("
- for the_element in array:
- converted += the_element + ";"
- converted = converted[:-1]
- converted += ")"
- else:
- converted = array[0]
- self.output = converted
- self.output_text.set(self.output)
- def copy_text_to_clipboard(self, text):
- self.master.clipboard_clear() # clear clipboard contents
- # append new value to clipbaord
- self.master.clipboard_append(text)
- def add_new_cell_function(self):
- self.cells[list(string.ascii_letters)[len(self.cells)]] = ''
- self.draw_cels()
- def draw_cels(self):
- i = 4
- self.cells_entry.clear()
- for key in self.cells:
- label = Label(self.master, text=key)
- label.grid(row=i, column=0)
- v = StringVar(value=self.cells[key])
- self.cells_entry["entry_"+key] = Entry(
- self.master, textvariable=v)
- self.cells_entry["entry_"+key].bind("<KeyRelease>", lambda event,
- key=key: self.update_cell(key))
- self.cells_entry["entry_"+key].grid(row=i, column=1)
- i += 1
- def update_cell(self, key):
- self.cells[key] = self.cells_entry["entry_"+key].get()
- root = Tk()
- my_gui = Converter(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement