Guest User

Untitled

a guest
Mar 7th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. try:
  2.     #py 3
  3.     import tkinter as tk
  4.     from tkinter import ttk
  5. except ImportError:
  6.     #py 2
  7.     import Tkinter as tk
  8.     import ttk as ttk
  9.  
  10. win = tk.Tk()
  11. win.geometry('850x450')
  12.  
  13.  
  14. nb_frames=2
  15.  
  16. combo_nb_frames = ttk.Combobox(values=["2","4",], width=2)
  17.  
  18. lbl = tk.Label(text="Number Of Frames")
  19. lbl.grid(column=1, row=1,padx=3,pady=1)
  20. combo_nb_frames.grid(column=2, row=1,padx=3,pady=3)
  21. combo_nb_frames.current(0)
  22.  
  23.  
  24. def initialise_frames(first_corner_x,first_corner_y,w,h,x_pad,nb_frames):
  25.     '''
  26.    Initialises all 4 frames
  27.    '''
  28.     frames_dict={}
  29.  
  30.     '''
  31.    Places an individual frame frame in the correct position
  32.    '''
  33.     for i in range(1,nb_frames+1):
  34.  
  35.         frame_dict = tk.Frame(width=w,height=h,relief='groove',borderwidth='2')
  36.  
  37.         frame_x=first_corner_x+w*(i-1)+x_pad*(i-1)    #horizontal position increases by width+pad for each i
  38.         frame_y=first_corner_y                        #vertical position doesn't change
  39.  
  40.  
  41.         frame_dict.place(x= frame_x,y= frame_y)
  42.  
  43.         frames_dict[i] = (frame_dict, (frame_x, frame_y)) #value is now a tuple containing the object, and the position
  44.  
  45.         current_frame=frames_dict[i][0]
  46.  
  47.         title_text= 'Frame ' + str(i)
  48.  
  49.         frame_label=tk.Label(current_frame,text=title_text,anchor="w")
  50.         frame_label.place(x=2,y=2)
  51.  
  52.         frame_name_label=tk.Label(current_frame,text='Name',anchor="w")
  53.         frame_name_label.place(x=2,y=35)
  54.  
  55.         frame_name_entry = tk.Entry(current_frame,width=15,bg="white")
  56.         frame_name_entry.place(x=45,y=35)
  57.  
  58.     return frames_dict
  59.  
  60. def hide_frames(frames_dict):
  61.     '''
  62.    Hides frames 3 and 4
  63.    '''
  64.     for i in [3,4]:
  65.  
  66.         frames_dict[i][0].place_forget()
  67.  
  68. def show_frames(frames_dict):
  69.     '''
  70.    Shows frames 3 and 4
  71.    '''
  72.     for i in [3, 4]:
  73.  
  74.         frames_dict[i][0].place(x = frames_dict[i][1][0], y = frames_dict[i][1][1])
  75.    
  76.    
  77.  
  78. def NbframesSelected(event2, frames_dict):
  79.     '''
  80.    frame selection event
  81.    '''
  82.     nb_frames=int(combo_nb_frames.get())
  83.     '''
  84.    Places / forgets the requested amount of frame frames
  85.    '''
  86.     if nb_frames==2:
  87.         hide_frames(frames_dict) #hides frames 3 and 4
  88.  
  89.     elif nb_frames==4:
  90.         show_frames(frames_dict)
  91.  
  92. #Set the params for the first frame position, the others are automatically 'shifted' as they are created
  93. first_corner_x=2
  94. first_corner_y=40
  95. w=200
  96. h=400
  97. x_pad=2
  98.  
  99. #Initalise all 4 frame frames, then hide frame 3 and 4
  100. frames_dict = initialise_frames(first_corner_x,first_corner_y,w,h,x_pad,4)
  101. hide_frames(frames_dict)
  102.  
  103. #Bind the event to the combo box button
  104. combo_nb_frames.bind("<<ComboboxSelected>>", lambda x: NbframesSelected(x, frames_dict))
  105.  
  106. win.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment