Setting up the widgets and controlling their appearance and location.
![Setting up the widgets and controlling their appearance and location.](http://www.java2s.com/Code/PythonImages/Settingupthewidgetsandcontrollingtheirappearanceandlocation.PNG)
from Tkinter import *
class MyApp:
def __init__(self, parent):
button_width = 6
button_padx = "2m"
button_pady = "1m"
buttons_frame_padx = "3m"
buttons_frame_pady = "2m"
buttons_frame_ipadx = "3m"
buttons_frame_ipady = "1m"
self.myParent = parent
self.buttons_frame = Frame(parent)
self.buttons_frame.pack(
ipadx=buttons_frame_ipadx,
ipady=buttons_frame_ipady,
padx=buttons_frame_padx,
pady=buttons_frame_pady,
)
self.button1 = Button(self.buttons_frame, command=self.button1Click)
self.button1.configure(text="OK", background= "green")
self.button1.focus_force()
self.button1.configure(
width=button_width,
padx=button_padx,
pady=button_pady
)
self.button1.pack(side=LEFT)
self.button1.bind("<Return>", self.button1Click_a)
self.button2 = Button(self.buttons_frame, command=self.button2Click)
self.button2.configure(text="Cancel", background="red")
self.button2.configure(
width=button_width,
padx=button_padx,
pady=button_pady
)
self.button2.pack(side=RIGHT)
self.button2.bind("<Return>", self.button2Click_a)
def button1Click(self):
if self.button1["background"] == "green":
self.button1["background"] = "yellow"
else:
self.button1["background"] = "green"
def button2Click(self):
self.myParent.destroy()
def button1Click_a(self, event):
self.button1Click()
def button2Click_a(self, event):
self.button2Click()
root = Tk()
myapp = MyApp(root)
root.mainloop()
Related examples in the same category
1. | Layout: anchor NW, W and E | | ![Layout: anchor NW, W and E](http://www.java2s.com/Code/PythonImages/LayoutanchorNWWandE.PNG) |
2. | Layout: anchor W side TOP | | ![Layout: anchor W side TOP](http://www.java2s.com/Code/PythonImages/LayoutanchorWsideTOP.PNG) |
3. | Layout: side TOP, LEFT | | ![Layout: side TOP, LEFT](http://www.java2s.com/Code/PythonImages/LayoutsideTOPLEFT.PNG) |
4. | Layout: side TOP and LEFT | | ![Layout: side TOP and LEFT](http://www.java2s.com/Code/PythonImages/LayoutsideTOPandLEFT.PNG) |
5. | Layout: LEFT LEFT and LEFT | | ![Layout: LEFT LEFT and LEFT](http://www.java2s.com/Code/PythonImages/LayoutLEFTLEFTandLEFT.PNG) |
6. | Layout: fit text side | | ![Layout: fit text side](http://www.java2s.com/Code/PythonImages/Layoutfittextside.PNG) |
7. | Layout: side TOP LEFT LEFT | | ![Layout: side TOP LEFT LEFT](http://www.java2s.com/Code/PythonImages/LayoutsideTOPLEFTLEFT.PNG) |
8. | Layout: frame fill BOTH expand YES | | ![Layout: frame fill BOTH expand YES](http://www.java2s.com/Code/PythonImages/LayoutframefillBOTHexpandYES.PNG) |
9. | Layout: pack side LEFT and expand YES | | ![Layout: pack side LEFT and expand YES](http://www.java2s.com/Code/PythonImages/LayoutpacksideLEFTandexpandYES.PNG) |
10. | Layout: TOP, CENTER and BOTTOM | | ![Layout: TOP, CENTER and BOTTOM](http://www.java2s.com/Code/PythonImages/LayoutTOPCENTERandBOTTOM.PNG) |
11. | Layout: top, center and bottom fill | | ![Layout: top, center and bottom fill](http://www.java2s.com/Code/PythonImages/Layouttopcenterandbottomfill.PNG) |
12. | Layout: side LEFT and fill | | ![Layout: side LEFT and fill](http://www.java2s.com/Code/PythonImages/LayoutsideLEFTandfill.PNG) |
13. | Layout: fill X | | ![Layout: fill X](http://www.java2s.com/Code/PythonImages/LayoutfillX.PNG) |
14. | Layout: fill X and Expand YES NO | | ![Layout: fill X and Expand YES NO](http://www.java2s.com/Code/PythonImages/LayoutfillXandExpandYESNO.PNG) |
15. | Layout: fill X and expand YES | | ![Layout: fill X and expand YES](http://www.java2s.com/Code/PythonImages/LayoutfillXandexpandYES.PNG) |
16. | Layout: side TOP and fill X | | ![Layout: side TOP and fill X](http://www.java2s.com/Code/PythonImages/LayoutsideTOPandfillX.PNG) |
17. | Use layout: fill | | ![Use layout: fill](http://www.java2s.com/Code/PythonImages/Uselayoutfill.PNG) |
18. | Use pack for a frame | | ![Use pack for a frame](http://www.java2s.com/Code/PythonImages/Usepackforaframe.PNG) |
19. | Set expand to YES and fill to BOTH | | ![Set expand to YES and fill to BOTH](http://www.java2s.com/Code/PythonImages/SetexpandtoYESandfilltoBOTH.PNG) |
20. | Add a label to the top of a frame | | ![Add a label to the top of a frame](http://www.java2s.com/Code/PythonImages/Addalabeltothetopofaframe.PNG) |
21. | Add a label to the center of a frame | | ![Add a label to the center of a frame](http://www.java2s.com/Code/PythonImages/Addalabeltothecenterofaframe.PNG) |
22. | Adds multi-widget layouts: TOP, RIGHT and LEFT | | ![Adds multi-widget layouts: TOP, RIGHT and LEFT](http://www.java2s.com/Code/PythonImages/AddsmultiwidgetlayoutsTOPRIGHTandLEFT.PNG) |
23. | Alternative packing/clipping order: LEFT, RIGHT and TOP | | ![Alternative packing/clipping order: LEFT, RIGHT and TOP](http://www.java2s.com/Code/PythonImages/AlternativepackingclippingorderLEFTRIGHTandTOP.PNG) |
24. | Creation order irrelevant to clipping | | ![Creation order irrelevant to clipping](http://www.java2s.com/Code/PythonImages/Creationorderirrelevanttoclipping.PNG) |
25. | Packing order and sides determine layout: make parents expandable | | ![Packing order and sides determine layout: make parents expandable](http://www.java2s.com/Code/PythonImages/Packingorderandsidesdeterminelayoutmakeparentsexpandable.PNG) |
26. | Use anchor to position, instead of fill to stretch | | ![Use anchor to position, instead of fill to stretch](http://www.java2s.com/Code/PythonImages/Useanchortopositioninsteadoffilltostretch.PNG) |
27. | Layout button in a row with different padx | | ![Layout button in a row with different padx](http://www.java2s.com/Code/PythonImages/Layoutbuttoninarowwithdifferentpadx.PNG) |
28. | Layout components in grid | | ![Layout components in grid](http://www.java2s.com/Code/PythonImages/Layoutcomponentsingrid.PNG) |
29. | Layout three button in a row | | ![Layout three button in a row](http://www.java2s.com/Code/PythonImages/Layoutthreebuttoninarow.PNG) |
30. | Pack side in TOP | | ![Pack side in TOP](http://www.java2s.com/Code/PythonImages/PacksideinTOP.PNG) |
31. | Nested containers | | ![Nested containers](http://www.java2s.com/Code/PythonImages/Nestedcontainers.PNG) |
32. | Creating a GUI object and associating it with its parent: packing, containers vs. widgets | | ![Creating a GUI object and associating it with its parent: packing, containers vs. widgets](http://www.java2s.com/Code/PythonImages/CreatingaGUIobjectandassociatingitwithitsparentpackingcontainersvswidgets.PNG) |
33. | Using the grid geometry manager | | ![Using the grid geometry manager](http://www.java2s.com/Code/PythonImages/Usingthegridgeometrymanager.PNG) |
34. | Set positions for components | | ![Set positions for components](http://www.java2s.com/Code/PythonImages/Setpositionsforcomponents.PNG) |
35. | Pack side RIGHT and LEFT | | ![Pack side RIGHT and LEFT](http://www.java2s.com/Code/PythonImages/PacksideRIGHTandLEFT.PNG) |
36. | Pack layout manager:Button component placed against top of window | | ![Pack layout manager:Button component placed against top of window](http://www.java2s.com/Code/PythonImages/PacklayoutmanagerButtoncomponentplacedagainsttopofwindow.PNG) |
37. | Pack layout manager: component placed against bottom of window, fills all available vertical and horizontal space | | ![Pack layout manager: component placed against bottom of window, fills all available vertical and horizontal space](http://www.java2s.com/Code/PythonImages/Packlayoutmanagercomponentplacedagainstbottomofwindowfillsallavailableverticalandhorizontalspace.PNG) |
38. | Component Placed against left side of window, fills all available horizontal space | | |
39. | Component Placed against right side of window, fills all available vertical space | | ![Component Placed against right side of window, fills all available vertical space](http://www.java2s.com/Code/PythonImages/ComponentPlacedagainstrightsideofwindowfillsallavailableverticalspace.PNG) |