GUI Layout Example I
One important aspect of GUI design is the layout of widgets in an application.
In this problem you will lay out two buttons in a frame. Note that, although this problem uses buttons, the same layout ideas apply to any widget (including other frames).
You need to write a function create_layout that takes a frame as its only argument and adds two buttons at the left of the frame. The first (leftmost) button should have the label “Button1”, and the other should have the label “Button2”. The callback (command) for both buttons should be the pressed function.
There is no need to create a tk app (the root window and frame will be initialised for you).
You must use pack for layout and not grid.
The frame should appear as depicted below.
When you run your code an extra window screen should pop up showing your layout. You can extend the window to enhance the image.
import tkinter as tk
def pressed():
“””
Button callback function (command).
You may alter what this function does if you wish, but you must not rename
or delete it.
“””
print(“Button Pressed!”)
def create_layout(frame):
“””
Add two buttons to the frame.
Both buttons should have the callback (command) pressed, and they should
have the labels “Button1” and “Button2”.
The layout in the frame after running this function will be:
+———————–+
|[Button1][Button2] |
+———————–+
Parameters:
frame (tk.Frame): The frame to create the two buttons in.
“””
GUI Layout Example II
We’re continuing the theme of laying out buttons in a frame. In this question, you’ll need to add fixed spacing between your widgets.
You need to write a function create_layout that takes a frame as its only argument and adds two buttons on top of each other in the frame. The first (top) button should have the label “Button1”, and the other should have the label “Button2”. The callback (command) for both buttons should be the pressed function.
The first button should have 20 pixels of space above and below it. The second button should have 20 pixels of internal space on its left and right.
Both buttons should appear on the far left of the frame.
As in the previous question, there is no need to create a tk app (the root window and frame will be initialised for you).
The frame should appear as depicted below.
When you run your code an extra window screen should pop up showing your layout. You can extend the window to enhance the image.
import tkinter as tk
def pressed():
“””
Button callback function (command).
You may alter what this function does if you wish, but you must not rename
or delete it.
“””
print(“Button Pressed!”)
def create_layout(frame):
“””
Add two buttons to the frame.
Both buttons should have the callback (command) pressed, and they should
have the labels “Button1” and “Button2”.
The layout in the frame after running this function will be:
+——————————+
| |(20px) |
|[Button1] <------------------ |
| |(20px) |
|[-(20px)-Button2-(20px)-] <-- |
| |
| |
+------------------------------+
Parameters:
frame (tk.Frame): The frame to create the two buttons in.
"""
GUI Layout Example III
We’re continuing the theme of laying out buttons in a frame. In this question, you’ll need to add fixed spacing between your widgets.
You need to write a function create_layout that takes a frame as its only argument and adds four buttons in the layout shown below.
The callback (command) for both buttons should be the pressed function.
This layout must be achieved using an additional Frame. You will probably want to look at the fill and expandkeyword arguments to pack.
As in the previous questions, there is no need to create a tk app (the root window and frame will be initialised for you).
When you run your code an extra window screen should pop up showing your layout. You can extend the window to enhance the image.
import tkinter as tk
def pressed():
"""
Button callback function (command).
You may alter what this function does if you wish, but you must not rename
or delete it.
"""
print("Button Pressed!")
def create_layout(frame):
"""
Add four buttons to the frame in the given formt.
Both buttons should have the callback (command) pressed, and they should
have the labels "Button1" through "Button4".
The layout in the frame after running this function will be:
+---------------------------------------+
| |
| [Button1] |
| [Button3] [Button4] |
| [Button2] |
| |
+---------------------------------------+
Parameters:
frame (tk.Frame): The frame to create the two buttons in.
"""