Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3.  
  4. # Change all label backgrounds
  5. def change_colour():
  6. c = user.get() #Get the entered text of the Entry widget
  7. for wid in widget_list:
  8. wid.configure(bg = c)
  9.  
  10. # Create GUI
  11. root = tk.Tk()
  12.  
  13. tk.Label(root, text='Enter a colour').pack()
  14.  
  15. user = tk.Entry(root)
  16. user.pack()
  17.  
  18. label_frame = tk.Frame(root)
  19. label_frame.pack()
  20.  
  21. btn = tk.Button(root, text='Change Colour', command = change_colour)
  22. btn.pack()
  23.  
  24. widget_list = [user, btn] # Add defined widgets to list
  25.  
  26. #Dynamicly create labels for example
  27. for x in range(10):
  28. lbl = tk.Label(label_frame, text='Label '+str(x))
  29. lbl.pack(side = tk.LEFT)
  30. widget_list.append(lbl) #Add widget object to list
  31.  
  32. root.mainloop()
  33.  
  34. Or if you have a Frame already containing all the widgets you want to change, then you can use this instead.
  35.  
  36. parent_widget.winfo_children() will return a list containing all the widgets stored inside the parent widget
  37.  
  38. def change_colour():
  39. c = user.get()
  40. for wid in label_frame.winfo_children():
  41. wid.configure(bg = c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement