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 functioncreate_layoutthat takes a frame as its only argument and adds two buttons at the left of the frame. The first (leftmost) button should have the labelButton1, and the other should have the labelButton2. The callback (command) for both buttons should be thepressedfunction.
There is no need to create a tk app (the root window and frame will be initialised for you).
You must usepackfor layout andnotgrid.
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
Were continuing the theme of laying out buttons in a frame. In this question, youll need to add fixed spacing between your widgets.
You need to write a functioncreate_layoutthat 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 labelButton1, and the other should have the labelButton2. The callback (command) for both buttons should be thepressedfunction.
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 IIIWere continuing the theme of laying out buttons in a frame. In this question, youll need to add fixed spacing between your widgets.You need to write a functioncreate_layoutthat takes a frame as its only argument and adds four buttons in the layout shown below.The callback (command) for both buttons should be thepressedfunction.This layout must be achieved using an additionalFrame. You will probably want to look at thefillandexpandkeyword arguments topack.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 tkdef pressed():”””Button callback function (command).You may alter what this function does if you wish, but you must not renameor 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 shouldhave 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.”””
Reviews
There are no reviews yet.