Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- #py 3
- import tkinter as tk
- from tkinter import ttk
- except ImportError:
- #py 2
- import Tkinter as tk
- import ttk as ttk
- win = tk.Tk()
- win.geometry('850x450')
- nb_frames=2
- combo_nb_frames = ttk.Combobox(values=["2","4",], width=2)
- lbl = tk.Label(text="Number Of Frames")
- lbl.grid(column=1, row=1,padx=3,pady=1)
- combo_nb_frames.grid(column=2, row=1,padx=3,pady=3)
- combo_nb_frames.current(0)
- def initialise_frames(first_corner_x,first_corner_y,w,h,x_pad,nb_frames):
- '''
- Initialises all 4 frames
- '''
- frames_dict={}
- '''
- Places an individual frame frame in the correct position
- '''
- for i in range(1,nb_frames+1):
- frame_dict = tk.Frame(width=w,height=h,relief='groove',borderwidth='2')
- frame_x=first_corner_x+w*(i-1)+x_pad*(i-1) #horizontal position increases by width+pad for each i
- frame_y=first_corner_y #vertical position doesn't change
- frame_dict.place(x= frame_x,y= frame_y)
- frames_dict[i] = (frame_dict, (frame_x, frame_y)) #value is now a tuple containing the object, and the position
- current_frame=frames_dict[i][0]
- title_text= 'Frame ' + str(i)
- frame_label=tk.Label(current_frame,text=title_text,anchor="w")
- frame_label.place(x=2,y=2)
- frame_name_label=tk.Label(current_frame,text='Name',anchor="w")
- frame_name_label.place(x=2,y=35)
- frame_name_entry = tk.Entry(current_frame,width=15,bg="white")
- frame_name_entry.place(x=45,y=35)
- return frames_dict
- def hide_frames(frames_dict):
- '''
- Hides frames 3 and 4
- '''
- for i in [3,4]:
- frames_dict[i][0].place_forget()
- def show_frames(frames_dict):
- '''
- Shows frames 3 and 4
- '''
- for i in [3, 4]:
- frames_dict[i][0].place(x = frames_dict[i][1][0], y = frames_dict[i][1][1])
- def NbframesSelected(event2, frames_dict):
- '''
- frame selection event
- '''
- nb_frames=int(combo_nb_frames.get())
- '''
- Places / forgets the requested amount of frame frames
- '''
- if nb_frames==2:
- hide_frames(frames_dict) #hides frames 3 and 4
- elif nb_frames==4:
- show_frames(frames_dict)
- #Set the params for the first frame position, the others are automatically 'shifted' as they are created
- first_corner_x=2
- first_corner_y=40
- w=200
- h=400
- x_pad=2
- #Initalise all 4 frame frames, then hide frame 3 and 4
- frames_dict = initialise_frames(first_corner_x,first_corner_y,w,h,x_pad,4)
- hide_frames(frames_dict)
- #Bind the event to the combo box button
- combo_nb_frames.bind("<<ComboboxSelected>>", lambda x: NbframesSelected(x, frames_dict))
- win.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment