Advertisement
here2share

# wutyewcie_tk_v2.py ^ Aug 5 2020

Aug 3rd, 2022 (edited)
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.27 KB | None | 0 0
  1. # wutyewcie_tk_v2.py
  2.  
  3. '''
  4. I suspect this project is far from complete whereas I also plan to make WUTYEWCIE Tk also create JavaScript code
  5. much as possible by menu selecting lines of python which should eventually prevent syntax errors so we can get a whole lot more work properly done. Always willing to accept help in any way to make WUTYEWCIE Tk hopefully super useful.
  6. '''
  7.  
  8. from tkinter import *
  9. ww = 1280
  10. hh = 620
  11. root = Tk()
  12. root.title('WUTYEWCIE Tk') # Pronounced as What-You-See-Toolkit
  13. root.state('zoomed')
  14. canvas = Canvas(width=ww, height=hh)
  15. canvas.pack()
  16. widgets = []
  17. def about():
  18.     messagebox.showinfo("About", "WUTYEWCIE Tk\nVersion: 1.0.0\nAuthors: Facebook Tkinter Groups\nDate: 2020-08-03")
  19. def import_file():
  20.     import_file = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
  21.     print(import_file)
  22. def import_url(): ### ZZZ
  23.     import_url = simpledialog.askstring("Input", "Enter URL:", parent=root)
  24.     print(import_url)
  25. def export_html():
  26.     export_html = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
  27.     print(export_html)
  28. def export_py():
  29.     ## save custom layout as a Python script
  30.     # loop through all added widgets to write the code
  31.     code = []
  32.     for widget in widgets:
  33.         code += ['# ' + widget.winfo_class()]
  34.         code += [widget.winfo_name() + ' = ' + widget_type + '(' + root.winfo_class() + ')']
  35.         for key, value in widget.config().items():
  36.             code += [widget.winfo_name() + '.' + key + ' = ' + repr(value)]
  37.         code += ['']
  38.     code = '\n'.join(code)
  39.     savefile = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("python files","*.py"),("all files","*.*")))
  40.     if savefile:
  41.         with open(savefile, 'w') as file:
  42.             file.write(code)
  43.         messagebox.showinfo('Success', 'File saved successfully')
  44.     else:
  45.         messagebox.showerror('Error', 'File not saved')
  46. def widget_options(widget):
  47.     code = []
  48.     code += ['# ' + widget.winfo_class()]
  49.     code += [widget.winfo_name() + ' = ' + widget_type + '(' + root.winfo_class() + ')']
  50.     for key, value in widget.config().items():
  51.         code += [widget.winfo_name() + '.' + key + ' = ' + repr(value)]
  52.     code = '\n'.join(code)
  53.     return code
  54. def import_code():
  55.     openfile = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("python files","*.py"),("all files","*.*")))
  56.     if openfile:
  57.         with open(openfile, 'r') as file:
  58.             code = file.read()
  59.         exec(code)
  60.     else:
  61.         messagebox.showerror('Error', 'File not found')
  62. options = []
  63. def widget_options(widget):
  64.     global options
  65.     i = 10
  66.     def iii(i=i):
  67.         i += 15
  68.         return i
  69.     options = []
  70.    
  71.     '''
  72.     # this section will have to be done very differently
  73.    
  74.     yscrollbar = Scrollbar(root)
  75.     yscrollbar.pack(side=RIGHT, fill=Y)
  76.     options_list = Listbox(root, yscrollcommand = yscrollbar.set, font=("Verdana", 8), bg="white", fg="blue")
  77.     options_list.place(x=160, y=i, width=160, height=hh-20)
  78.     yscrollbar.config(command = options_list.yview)
  79.     for option in widget.keys():
  80.         if '.' not in option:
  81.             #entry_form = Entry(option_label, font=("Verdana", 8), bg="white", fg="blue")
  82.             #entry_form.place(x=i, y=iii(), width=145, height=15)
  83.             options.append(option)
  84.     # print(options)
  85.     # options_list.insert(END, option) <-- forget this method
  86.     '''
  87. def more_options(event):
  88.     0 # displays all othe widget options, but... in a TopWindow instead
  89. # import and export dropdow-menu
  90. menu = Menu(root)
  91. root.config(menu=menu)
  92. subMenu = Menu(menu)
  93. menu.add_cascade(label='Import', menu=subMenu)
  94. subMenu.add_command(label='Import Python Script', command=import_file)
  95. subMenu.add_command(label='Import URL*', command=import_url)
  96. subMenu.add_separator()
  97. subMenu.add_command(label='Export Python Script', command=export_py)
  98. subMenu.add_command(label='Export HTML5*/JS', command=export_html)
  99. subMenu = Menu(menu)
  100. menu.add_cascade(label='Help', menu=subMenu)
  101. subMenu.add_command(label='About', command=about)
  102. subMenu.add_command(label='Exit', command=root.quit)
  103. ## create equal length draggable widgets
  104. # box label
  105. box_label = Label(canvas, text='Label', bg='green', fg='white', font=('Arial', 12))
  106. box_label.place(x=10, y=10, width=140, height=27)
  107. # button label
  108. button_label = Label(canvas, text='Button', bg='green', fg='white', font=('Arial', 12))
  109. button_label.place(x=10, y=40, width=140, height=27)
  110. # checkbox label
  111. checkbox_label = Label(canvas, text='CheckButton', bg='green', fg='white', font=('Arial', 12))
  112. checkbox_label.place(x=10, y=70, width=140, height=27)
  113. # radiobutton label
  114. radiobutton_label = Label(canvas, text='RadioButton', bg='green', fg='white', font=('Arial', 12))
  115. radiobutton_label.place(x=10, y=100, width=140, height=27)
  116. # entry label
  117. entry_label = Label(canvas, text='Entry', bg='green', fg='white', font=('Arial', 12))
  118. entry_label.place(x=10, y=130, width=140, height=27)
  119. # option_label label
  120. listbox_label = Label(canvas, text='ListBox', bg='green', fg='white', font=('Arial', 12))
  121. listbox_label.place(x=10, y=160, width=140, height=27)
  122. # message label
  123. message_label = Label(canvas, text='Message', bg='green', fg='white', font=('Arial', 12))
  124. message_label.place(x=10, y=190, width=140, height=27)
  125. # to see more options
  126. more_options_label = Label(canvas, text='Primary Widget Options', bg=None, fg='black', font=('Arial', 9))
  127. more_options_label.place(x=6, y=225, width=140, height=20)
  128. # to see more options
  129. more_options_label = Label(canvas, text='TO SEE MORE OPTIONS', bg='orange', fg='white', font=('Arial', 8)) # <-- will be grayed out when widgets are unselected
  130. more_options_label.place(x=10, y=hh-24, width=140, height=20)
  131.  
  132. def wxy():
  133.     x,y = root.winfo_x()+5, root.winfo_y()+60
  134.     return x,y
  135. def xy():
  136.     x,y = root.winfo_pointerxy()
  137.     return x,y
  138. def on_start(event):
  139.     global __widget
  140.     __widget = 0
  141. def on_start_2(event):
  142.     global __widget
  143.     __widget = event.widget
  144. def on_drag(event):
  145.     global __widget
  146.     x,y = xy()
  147.     wx,wy = wxy()
  148.     if x-wx-15 > x1:
  149.         if not __widget:
  150.             widget = event.widget
  151.             ## enable dragging a copy selected of widget onto label GUI_area to place it
  152.             # get x1,y1,x2,y2 of GUI_area
  153.             widget_type = widget.cget("text").title()
  154.             new_widget = eval(widget_type + '()')
  155.             if widget_type in ["Button", "Label"]:
  156.                 new_widget.place(x=x-wx-10, y=y-wy, width=widget.winfo_width()+10, height=widget.winfo_height()+10)
  157.             else:
  158.                 new_widget.place(x=x-wx-10, y=y-wy)
  159.             __widget = new_widget
  160.             new_widget.bind("<ButtonPress-1>", on_start_2)
  161.             new_widget.bind("<B1-Motion>", on_drag_2)
  162.             new_widget.bind("<ButtonRelease-1>", on_drop)
  163.             widgets.append(new_widget)
  164.             widget_options(new_widget)
  165.     if __widget:
  166.         __widget.place(x=x-wx-10, y=y-wy)
  167.        
  168. def on_drag_2(event):
  169.     x,y = xy()
  170.     wx,wy = wxy()
  171.     widget = event.widget
  172.     widget.place(x=x-wx-10, y=y-wy)
  173. def on_drop(event):
  174.     x,y = xy()
  175.     wx,wy = wxy()
  176.     if __widget:
  177.         if x-wx-15 < x1:
  178.             print(widgets)
  179.             print(__widget)
  180.             __widget.destroy()
  181.             widgets.remove(__widget)
  182.             print("deleted widget")
  183.         else:
  184.             __widget.place(x=x-wx-10, y=y-wy)
  185.             print("placed widget")
  186.  
  187. x1 = 150
  188. y1 = 5
  189. x2 = ww-x1-10
  190. y2 = hh-5
  191. GUI_area = Label(canvas, bg='#8470FF', fg='black', relief='sunken')
  192. GUI_area.place(x=x1, y=y1, width=x2, height=y2)
  193.  
  194. # define drag-and-drop of place() labels
  195. box_label.bind("<ButtonPress-1>", on_start)
  196. box_label.bind("<B1-Motion>", on_drag)
  197. box_label.bind("<ButtonRelease-1>", on_drop)
  198. button_label.bind("<ButtonPress-1>", on_start)
  199. button_label.bind("<B1-Motion>", on_drag)
  200. button_label.bind("<ButtonRelease-1>", on_drop)
  201. checkbox_label.bind("<ButtonPress-1>", on_start)
  202. checkbox_label.bind("<B1-Motion>", on_drag)
  203. checkbox_label.bind("<ButtonRelease-1>", on_drop)
  204. radiobutton_label.bind("<ButtonPress-1>", on_start)
  205. radiobutton_label.bind("<B1-Motion>", on_drag)
  206. radiobutton_label.bind("<ButtonRelease-1>", on_drop)
  207. entry_label.bind("<ButtonPress-1>", on_start)
  208. entry_label.bind("<B1-Motion>", on_drag)
  209. entry_label.bind("<ButtonRelease-1>", on_drop)
  210. listbox_label.bind("<ButtonPress-1>", on_start)
  211. listbox_label.bind("<B1-Motion>", on_drag)
  212. listbox_label.bind("<ButtonRelease-1>", on_drop)
  213. message_label.bind("<ButtonPress-1>", on_start)
  214. message_label.bind("<B1-Motion>", on_drag)
  215. message_label.bind("<ButtonRelease-1>", on_drop)
  216. more_options_label.bind("<ButtonPress-1>", more_options)
  217.  
  218. #
  219. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement