Advertisement
Fire-Flowey

FigureCreate

Jan 2nd, 2019
1,662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.81 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. class MainWindow:
  4.     def __init__(self, master):
  5.         self.master = master
  6.         self.master.title("Нарисуй-ка мне фигуру!")
  7.         self.master.resizable(False, False)
  8.         self.width, self.height = self.master.winfo_screenwidth() // 2 - 250, self.master.winfo_screenheight() // 2 - 268
  9.         self.master.geometry('500x536+{}+{}'.format(self.width, self.height))
  10.  
  11.         self.canvas = Canvas(width=500, height=500, bg="white")
  12.         window_create = Button(width=40, height=1, text="Добавить фигуру",
  13.                            command= lambda: FigureWindow(self, self.get_coords()))
  14.         self.canvas.pack()
  15.         window_create.pack()
  16.     def get_coords(self):
  17.         self.master.update_idletasks()
  18.         coords = self.master.geometry()
  19.         coords = coords.split('+')
  20.         width_window, height_window = int(coords[1]), int(coords[2])
  21.         return [width_window, height_window]
  22.  
  23.  
  24. class FigureWindow:
  25.     def __init__(self, main_window, w_h_array): # Создаём окно
  26.         self.main = main_window
  27.         self.window = Toplevel(main_window.master)
  28.         w, h = w_h_array[0], w_h_array[1]
  29.         self.window.geometry("190x165+{}+{}".format(510+w, h))  # Формируем окно
  30.         self.window.title("Фигура")
  31.         self.window.resizable(False, False)
  32.  
  33.         self.typeVar= IntVar() # Переменная
  34.         self.typeVar.set(0)
  35.  
  36.         self.colors = ["black", "white", "snow", "linen", "lavender", "misty rose", "gray", "blue", "cyan", "yellow", "gold",
  37.                        "coral", "red", "purple", "khaki", "thistle", "indian red"]
  38.         self.outline_color, self.fill_color = self.colors[0], self.colors[1] # Цвета
  39.  
  40.         frames, elements = self.window_content(self.window) # Контект окна
  41.         self.pack_content(frames, elements) # Упаковка окна
  42.         self.bind_events(elements) # Подсоединение событий
  43.     def window_content(self, window): # Заполняем окно
  44.         frame_global = Frame(window)
  45.         frame_dot_one, frame_dot_two, frame_figure, \
  46.         frame_fill_color, frame_outline_color = (Frame(frame_global) for i in range(5))
  47.  
  48.         x1, y1 = Label(frame_dot_one, text="x1"), Label(frame_dot_one, text="y1")
  49.         x2, y2 = Label(frame_dot_two, text="x2"), Label(frame_dot_two, text="y2")
  50.         self.x1_entry, self.y1_entry = Entry(frame_dot_one, width=5), Entry(frame_dot_one, width=5)
  51.         self.x2_entry, self.y2_entry = Entry(frame_dot_two, width=5), Entry(frame_dot_two, width=5)
  52.  
  53.         square = Radiobutton(frame_figure, text="Прямоугольник", variable=self.typeVar, value=0)
  54.         oval = Radiobutton(frame_figure, text="Овал", variable=self.typeVar, value=1)
  55.  
  56.         fill_label = Label(frame_fill_color, text="Заливка: ")
  57.         outline_label = Label(frame_outline_color, text="Грани:     ")
  58.         self.color_fill = Listbox(frame_fill_color, width=210, height=1)
  59.         self.color_outline = Listbox(frame_outline_color, width=210, height=1)
  60.         for i in self.colors:
  61.             self.color_outline.insert(END, i)
  62.             if i == "white":
  63.                 self.color_fill.insert(0, i)
  64.             else:
  65.                 self.color_fill.insert(END, i)
  66.  
  67.         draw_button = Button(frame_global, text="Нарисовать фиругу",
  68.             command= lambda: self.create_figure((self.x1_entry, self.y1_entry, self.x2_entry, self.y2_entry)))
  69.  
  70.         return ((frame_dot_one, frame_dot_two, frame_figure, frame_fill_color, frame_outline_color, frame_global),
  71.                 ((x1, self.x1_entry, y1, self.y1_entry), (x2, self.x2_entry, y2, self.y2_entry),
  72.                 (square, oval),(fill_label, self.color_fill), (outline_label, self.color_outline), draw_button))
  73.     def pack_content(self, frames, elements): # Упаковываем окно
  74.         for i in frames: i.pack()
  75.         for i in range(len(elements)):
  76.             if isinstance(elements[i], tuple):
  77.                 length = len(elements[i])
  78.             else:
  79.                 length = 1
  80.             for j in range(length):
  81.                 if length == 1:
  82.                     elements[i].pack(anchor=N)
  83.                 elif i == 2:
  84.                     elements[i][j].pack(anchor=W)
  85.                 else:
  86.                     elements[i][j].pack(side=LEFT)
  87.     def bind_events(self, elements): # Подсоединяем события
  88.         self.color_fill.bind("<FocusIn>", lambda event: self.list_select(self.color_fill))
  89.         self.color_outline.bind("<FocusIn>", lambda event: self.list_select(self.color_outline))
  90.         self.color_fill.bind("<Return>", lambda event: self.list_change_item(self.color_fill))
  91.         self.color_outline.bind("<Return>", lambda event: self.list_change_item(self.color_outline))
  92.         self.color_fill.bind("<FocusOut>", lambda event: self.list_deselect(listbox=self.color_fill))
  93.         self.color_outline.bind("<FocusOut>", lambda event: self.list_deselect(listbox=self.color_outline))
  94.         for i in range(len(elements)):
  95.             if i == len(elements)-1:
  96.                 elements[i].bind("<FocusOut>", lambda event: self.list_deselect(Listbox()))
  97.             elif i == 3 or i == 4:
  98.                 pass
  99.             else:
  100.                 for j in range(len(elements[i])):
  101.                     elements[i][j].bind("<Button-1>", lambda event: self.list_deselect(Listbox()))
  102.     def list_select(self, listbox):
  103.         listbox["height"] = len(self.colors)
  104.         self.window.geometry("190x{}".format(165+len(self.colors)*15))
  105.     def list_deselect(self, listbox):
  106.         if listbox != self.color_fill and listbox != self.color_outline:
  107.             self.color_fill["height"] = 1
  108.             self.color_outline["height"] = 1
  109.         else:
  110.             listbox["height"] = 1
  111.         self.window.geometry("190x165")
  112.     def list_change_item(self, listbox):
  113.         if   listbox == self.color_fill:
  114.             self.fill_color = listbox.get(listbox.curselection())
  115.         elif listbox == self.color_outline:
  116.             self.outline_color = listbox.get(listbox.curselection())
  117.         listbox.insert(0, listbox.get(listbox.curselection()))
  118.         listbox.delete(listbox.curselection())
  119.         self.list_deselect(listbox)
  120.     def create_figure(self, elements):
  121.         x1, y1, x2, y2 = (elements[i].get() for i in range(4))
  122.         if self.typeVar.get():
  123.             self.main.canvas.create_oval((x1, y1), (x2, y2), fill=self.color_fill.get(0), outline=self.color_outline.get(0))
  124.         else:
  125.             self.main.canvas.create_rectangle((x1, y1), (x2, y2), fill=self.color_fill.get(0), outline=self.color_outline.get(0))
  126.         self.window.destroy()
  127.  
  128. def main():
  129.     root = Tk()
  130.     MainWindow(root)
  131.     root.mainloop()
  132.  
  133. if __name__ == "__main__":
  134.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement