Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. from tkinter import Tk, Label, Button, Entry, StringVar, W, E, END
  2. import string
  3.  
  4.  
  5. class Converter:
  6. def __init__(self, master=Tk):
  7. self.master = master
  8. self.master.title = "Converter"
  9.  
  10. self.output = ""
  11. self.cells = {
  12. 'a': 'A1',
  13. 'b': 'B1',
  14. 'c': 'C1',
  15. 'd': 'D1',
  16. }
  17.  
  18. self.cells_entry = {}
  19.  
  20. self.output_text = StringVar()
  21. self.output_text.set(self.output)
  22.  
  23. self.output_label = Label(master, textvariable=self.output_text)
  24. self.label = Label(master, text="Output:")
  25.  
  26. self.entry = Entry(master)
  27.  
  28. self.convert_button = Button(
  29. master, text="Convert", command=lambda: self.convert(self.entry.get(), []))
  30. self.copy_button = Button(
  31. master, text="Copy", command=lambda: self.copy_text_to_clipboard(self.output))
  32. self.add_new_cell = Button(
  33. master, text="Add new input", command=lambda: self.add_new_cell_function())
  34.  
  35. self.label.grid(row=0, column=0, sticky=W)
  36. self.output_label.grid(row=0, column=1, columnspan=3, sticky=E)
  37. self.copy_button.grid(row=1, column=0)
  38.  
  39. self.entry.grid(row=2, column=0, columnspan=3, sticky=W + E)
  40. self.convert_button.grid(row=2, column=4)
  41.  
  42. self.add_new_cell.grid(row=3, column=0)
  43.  
  44. self.draw_cels()
  45.  
  46. def convert(self, string, cells):
  47. print(self.cells["b"])
  48. for key in self.cells:
  49. string = string.replace(key, self.cells[key])
  50.  
  51. sums = string.split('+')
  52. converted = ""
  53.  
  54. array = []
  55.  
  56. for the_sum in sums:
  57. products = the_sum.split('*')
  58. if len(products) > 1:
  59. expression = "AND("
  60. for product in products:
  61. if product[0] == '!':
  62. product = product.replace('!', '')
  63. product = f"NOT({product})"
  64. expression += product + ";"
  65. expression = expression[:-1]
  66. expression += ")"
  67. array.append(expression)
  68. else:
  69. array.append(the_sum)
  70.  
  71. if len(array) > 1:
  72. converted += "OR("
  73. for the_element in array:
  74. converted += the_element + ";"
  75. converted = converted[:-1]
  76. converted += ")"
  77. else:
  78. converted = array[0]
  79.  
  80. self.output = converted
  81. self.output_text.set(self.output)
  82.  
  83. def copy_text_to_clipboard(self, text):
  84. self.master.clipboard_clear() # clear clipboard contents
  85. # append new value to clipbaord
  86. self.master.clipboard_append(text)
  87.  
  88. def add_new_cell_function(self):
  89. self.cells[list(string.ascii_letters)[len(self.cells)]] = ''
  90. self.draw_cels()
  91.  
  92. def draw_cels(self):
  93. i = 4
  94. self.cells_entry.clear()
  95. for key in self.cells:
  96. label = Label(self.master, text=key)
  97. label.grid(row=i, column=0)
  98. v = StringVar(value=self.cells[key])
  99.  
  100. self.cells_entry["entry_"+key] = Entry(
  101. self.master, textvariable=v)
  102.  
  103. self.cells_entry["entry_"+key].bind("<KeyRelease>", lambda event,
  104. key=key: self.update_cell(key))
  105. self.cells_entry["entry_"+key].grid(row=i, column=1)
  106. i += 1
  107.  
  108. def update_cell(self, key):
  109. self.cells[key] = self.cells_entry["entry_"+key].get()
  110.  
  111.  
  112. root = Tk()
  113. my_gui = Converter(root)
  114. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement