Creating a phone-like user interface using Python's Tkinter library involves designing a simple graphical layout that resembles a smartphone screen. Tkinter provides all the necessary widgets and tools to create such an interface, including buttons, labels, and frames. Below is an example of how you can create a basic phone-like interface with a simple dial pad.
Install Tkinter : If you're using Python 3, Tkinter is included by default. If not, you'll need to install it.
Design the Interface : We'll create a main window that represents the phone screen, and place buttons in a grid that resembles a dial pad.
Here’s a simple implementation to get you started:
python Copy code
import tkinter as tk
class PhoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Phone Interface")
self.root.geometry("300x400")
# Creating the screen for number display
self.screen = tk.Entry(self.root, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.screen.grid(row=0, column=0, columnspan=4)
# Adding buttons
buttons = [
'1', '2', '3',
'4', '5', '6',
'7', '8', '9',
'*', '0', '#'
]
row_val = 1
col_val = 0
# Dynamically create the buttons and place them in the grid
for button in buttons:
action = lambda x=button: self.press_button(x)
tk.Button(
self.root,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=action
).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 2:
col_val = 0
row_val += 1
# Adding a Call button
action = lambda x='CALL': self.press_button(x)
tk.Button(
self.root,
text='CALL',
padx=20,
pady=20,
font=('Arial', 18),
fg='white',
bg='green',
command=action
).grid(row=row_val, column=0, columnspan=3)
def press_button(self, button):
current_value = self.screen.get()
if button == 'CALL':
self.screen.delete(0, tk.END)
self.screen.insert(0, "Calling...")
else:
self.screen.delete(0, tk.END)
self.screen.insert(0, current_value + button)
if __name__ == "__main__":
root = tk.Tk()
app = PhoneInterface(root)
root.mainloop()
Explanation:
Import tkinter : We start by importing the tkinter module.
Create the Main Window : We initialize the main window which represents the phone screen.
Screen Widget : An Entry widget is used to display the numbers dialed by the user.
Buttons Initialization : We create a list of button labels to simplify the creation process of the number buttons.
Grid Layout : The buttons are placed using the grid method. The call button spans all columns in the final row.
Button Functionality : Each button's command is set to a lambda function that calls press_button with the appropriate value.
Call Button : There's a special check for when the 'CALL' button is pressed.
This is a basic example and can be expanded with more features like backspace, actual calling logic, and more sophisticated UI components.
tkinter smart phone like user interface
Assistant2024-06-29 09:38 UTC Copy response
Creating a more sophisticated smartphone-like user interface in Python using Tkinter involves adding more complex elements such as multiple screens (like a home screen, contacts, messaging, etc.), icons, navigation bars, and possibly interaction animations.
Below is a step-by-step example to create a basic smartphone-like interface with a home screen and an example app screen (such as a simple calculator app).
Install Tkinter : Make sure you’re using Python 3, as Tkinter is included by default.
Designing Multiple Screens : Use frames to represent different screens and switch between them based on user actions.
Here's a detailed implementation to demonstrate this:
Assistant2024-06-29 09:42 UTC Copy response
To integrate an SMS app into the smartphone-like interface using Tkinter, we'll create another frame for the SMS app. This frame will include basic functionalities for viewing messages and sending new messages.
Here’s how you can modify the previous implementation to include an SMS app:
Create an SMS App Frame : Similar to the calculator app frame.
Modify the Home Screen : Add a button to navigate to the SMS app frame.
Implement SMS App Logic : This will include a text area to display messages and a text entry to compose a new message.
Assistant2024-06-29 09:48 UTC Copy response
Improving the layout to be more visually appealing and organized can significantly enhance the user experience. Here are several steps you can take to improve the layout of the smartphone-like UI:
Design Consistency : Ensure that the design is consistent across different screens.
Button Styling : Use custom styles for buttons to make them look more attractive.
Grid Configuration : Use better grid configurations to structure the elements properly.
Padding and Margins : Use appropriate padding and margins for better spacing.
Responsive Design : Adjust the layout to make it more responsive to different screen sizes.
Add Icons : Optionally, you can add icons to buttons for a more realistic smartphone interface.
Here's an improved version of the previous implementation:
reduce size of calculator and sms button to make it is within the boundary of the root window
Assistant2024-06-29 09:55 UTC Copy response
To ensure that the buttons fit well within the boundary of the root window, we can adjust the size and padding of the buttons. Additionally, we can use a grid-based layout more effectively to control the spacing and positioning.
Here is the updated code to adjust the size of the Calculator and SMS buttons to fit within the window without overflowing:
make the home screen to fill root window
Assistant2024-06-29 10:06 UTC Copy response
To make the home screen (and other screens) fill the root window, you can use the grid geometry manager more effectively. By configuring the grid's row and column weights, you can ensure that the frames and their content expand to fill the available space in the root window.
Changes:
Use grid to place the main frame and configure its weights.
Ensure all content within the frames is aligned and fills the space properly.
Here's the updated version:
Show full message · 209 lines Assistant2024-06-29 10:12 UTC Copy response
I'm glad to hear that the improved layout works better for you! If you have any additional features or more specific customizations you want to add to your smartphone-like interface, feel free to ask.
Here are a few additional tips and enhancements you might consider to further improve the interface:
Icons : Adding icons to buttons can make the interface more intuitive. You can use the PhotoImage widget in Tkinter to load images.
Animations : Implement simple animations for transitions between screens to enhance user experience.
Themes : Consider adding a dark mode or different themes to make the interface visually appealing and user-friendly.
Responsive Design : Make sure the interface looks good on different devices and screen sizes by testing and adjusting grids and weights accordingly.
Additional Apps : Enhance the interface by adding more apps like a contacts app or a simple browser.
If you need more specific guidance on any of these or other features, feel free to ask! Here's an example of adding icons to the home screen buttons:
Show full message · 232 lines AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
Assistant2024-06-29 10:23 UTC Copy response
The ANTIALIAS attribute was removed in Pillow version 10.0.0 and replaced with Image.Resampling.LANCZOS. If you're using a version of Pillow that is 10.0.0 or newer, you'll need to use the updated constants.
Here’s the updated code to correct the attribute error:
Ensure Proper Resampling : Use Image.Resampling.LANCZOS instead of Image.ANTIALIAS.
Show full message · 229 lines Assistant2024-06-29 10:32 UTC Copy response
To add a header and footer to your smartphone-like user interface, you can create additional frames for these sections within the main window. This will enhance the structure and organization of the interface. Here's how you can do it:
Create Header and Footer Frames : Add top and bottom frames for the header and footer.
Add Widgets to Header and Footer : Populate these frames with appropriate widgets like labels and buttons.
Adjust Main Content Area : Ensure that the main content area adjusts dynamically based on the presence of the header and footer.
Here's the updated code with header and footer:
Show full message · 247 lines Assistant2024-06-29 10:58 UTC Copy response
To further improve the layout, we'll enhance the visual design of the header and footer, ensure better spacing and alignment, and make sure that the main content is positioned and sized appropriately.
Here are the steps to create a more polished layout:
Enhanced Header and Footer : Add icons, improved spacing, and alignment.
Consistent Padding and Margin : Ensure that all elements have consistent padding and margins.
Responsive Grid Configuration : Use the grid configuration effectively to make sure the layout adjusts appropriately based on the window size.
Refine Content Area : Ensure that the main content area is properly positioned and the content inside is well-aligned.
Here is the improved version of the code:
Show full message · 258 lines Assistant2024-06-29 11:08 UTC Copy response
I'm sorry to hear that the layout still didn't meet your expectations. Let's try again and make further improvements. We'll focus on creating a more visually appealing interface with better alignment, spacing, and responsiveness.
Step-by-step Breakdown:
Header and Footer : Ensure they are fixed and styled properly.
Main Content Area : Adjust padding and placement for consistency.
Improved Button Placement and Appearance : Neatly organize buttons and make them more visually appealing.
Here's another iteration, focusing on creating a consistent and polished look:
Show full message · 201 lines buttons to big they do not fit in the home screen
Assistant2024-06-29 11:20 UTC Copy response
I apologize for the inconvenience caused. Let's reduce the size of the buttons in the home screen to fit properly and ensure the layout is compact and visually appealing.
Here's an improved version of the code:
Show full message · 187 lines Assistant2024-06-29 12:21 UTC Copy response
Adding SMS notifications to the interface requires a way to indicate new messages to the user. We can achieve this by updating the header to show a notification badge when a new SMS is received.
Steps to Implement:
Create a notification badge in the header.
Update the badge when a new message is sent.
Clear the notification when the user views the messages.
Updated Code:
Show full message · 210 lines
Turns (13) import tkinter as tk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.create_frames()
self.create_home_screen()
self.create_calculator_app()
self.show_frame('home_screen')
def create_frames(self):
self.frames = {}
for frame_name in ['home_screen', 'calculator']:
frame = tk.Frame(self.root)
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
title = tk.Label(frame, text="Home Screen", font=('Arial', 24))
title.pack(pady=10)
calc_button = tk.Button(
frame,
text="Calculator",
font=('Arial', 18),
command=lambda: self.show_frame('calculator'))
calc_button.pack(pady=20)
# Add more app buttons as needed
def create_calculator_app(self):
frame = self.frames['calculator']
title = tk.Label(frame, text="Calculator", font=('Arial', 24))
title.grid(row=0, column=0, columnspan=4, pady=10)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20)
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Explanation:
Frames Setup : We set up frames to represent different screens of the smartphone interface.
Navigation Logic : We use the show_frame method to switch between frames.
Home Screen : This frame acts as the main menu with buttons to launch different apps.
Calculator App : This frame contains a simple calculator layout with buttons and an entry widget for the display.
This is a basic skeleton of a smartphone-like interface, which you can expand by adding more complex apps, a navigation bar, more sophisticated styling, and animations as needed. Each button triggers navigation to another frame or performs actions relevant to the selected app.
import tkinter as tk
from tkinter import scrolledtext
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_frames()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_frames(self):
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.root)
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
title = tk.Label(frame, text="Home Screen", font=('Arial', 24))
title.pack(pady=10)
calc_button = tk.Button(
frame,
text="Calculator",
font=('Arial', 18),
command=lambda: self.show_frame('calculator'))
calc_button.pack(pady=20)
sms_button = tk.Button(
frame,
text="SMS",
font=('Arial', 18),
command=lambda: self.show_frame('sms'))
sms_button.pack(pady=20)
def create_calculator_app(self):
frame = self.frames['calculator']
title = tk.Label(frame, text="Calculator", font=('Arial', 24))
title.grid(row=0, column=0, columnspan=4, pady=10)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20)
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
title = tk.Label(frame, text="SMS", font=('Arial', 24))
title.grid(row=0, column=0, columnspan=2, pady=10)
self.sms_display = scrolledtext.ScrolledText(frame, wrap=tk.WORD, font=('Arial', 14))
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='news')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=5, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
command=self.send_message)
send_button.grid(row=2, column=1, padx=5, pady=5, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=3, column=0, columnspan=2, pady=10)
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
# Normally, you'd handle the message sending logic here (e.g., API call)
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Explanation:
SMS Frame and Functionality :
A new frame named sms is created for the SMS app.
A ScrolledText widget is used to display the messages, allowing scrolling if the messages go beyond the available space.
An Entry widget and a Send button are used for composing and sending new messages.
The send_message method handles sending the message by appending it to the messages list and updating the display.
Home Screen :
The home screen now includes a button to navigate to the SMS app frame.
Update SMS Display :
The update_sms_display method refreshes the SMS display area with the current list of messages.
By following this structure, you can easily add more apps and screens and navigate between them, forming a basic smartphone-like user interface. You can further enhance the functionality by incorporating more complex features and better UI/UX designs.
import tkinter as tk
from tkinter import scrolledtext
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_frames()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_frames(self):
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.root, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.pack(pady=20)
button_frame = tk.Frame(frame, bg="#f0f0f0")
button_frame.pack(pady=20)
calc_button = tk.Button(
button_frame,
text="Calculator",
font=('Arial', 18),
width=15,
height=3,
bg="#4CAF50",
fg="white",
command=lambda: self.show_frame('calculator'))
calc_button.grid(row=0, column=0, padx=10, pady=10)
sms_button = tk.Button(
button_frame,
text="SMS",
font=('Arial', 18),
width=15,
height=3,
bg="#2196F3",
fg="white",
command=lambda: self.show_frame('sms'))
sms_button.grid(row=0, column=1, padx=10, pady=10)
def create_calculator_app(self):
frame = self.frames['calculator']
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=20)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
width=5,
height=2,
command=command
).grid(row=row_val, column=col_val, padx=5, pady=5)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20)
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20)
self.sms_display = scrolledtext.ScrolledText(frame, wrap=tk.WORD, font=('Arial', 14), height=20, state=tk.DISABLED)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='news')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
bg="#009688", fg="white",
command=self.send_message)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=3, column=0, columnspan=2, pady=20)
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
# Normally, you'd handle the message sending logic here (e.g., API call)
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Improvements Made:
Background Color : Set a background color for frames to maintain consistency.
Padding and Spacing : Added padding and spacing around buttons and elements for a neater layout.
Button Styling : Applied custom styles to buttons including background and foreground colors.
Grid Configuration : Properly configured the grid for better structural layout.
Consistency : Maintained consistent design, font, and colors across all screens.
Home Screen Buttons : Arranged home screen buttons side by side.
Next Steps: These improvements should provide a more visually appealing and organized interface. You can further enhance it by:
Adding Icons : Use images or icons for buttons to make them more visually appealing.
Incorporating Animations : Add animations for transitions between screens for a smoother user experience.
Theme Customization : Allow users to switch between light and dark themes or customize colors.
Responsiveness : Test on different screen resolutions and adjust layouts accordingly.
By following these steps, you can create a more engaging and user-friendly smartphone-like interface using Tkinter.
import tkinter as tk
from tkinter import scrolledtext
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_frames()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_frames(self):
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.root, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.pack(pady=20)
button_frame = tk.Frame(frame, bg="#f0f0f0")
button_frame.pack(pady=10)
calc_button = tk.Button(
button_frame,
text="Calculator",
font=('Arial', 18),
width=10,
height=3,
bg="#4CAF50",
fg="white",
command=lambda: self.show_frame('calculator'))
calc_button.grid(row=0, column=0, padx=10, pady=10)
sms_button = tk.Button(
button_frame,
text="SMS",
font=('Arial', 18),
width=10,
height=3,
bg="#2196F3",
fg="white",
command=lambda: self.show_frame('sms'))
sms_button.grid(row=0, column=1, padx=10, pady=10)
def create_calculator_app(self):
frame = self.frames['calculator']
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=20)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val, padx=5, pady=5)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20)
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20)
self.sms_display = scrolledtext.ScrolledText(frame, wrap=tk.WORD, font=('Arial', 14), height=20, state=tk.DISABLED)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='news')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
bg="#009688", fg="white",
command=self.send_message)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=3, column=0, columnspan=2, pady=20)
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
# Normally, you'd handle the message sending logic here (e.g., API call)
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Changes:
Button Size : Reduced the width and height of the Calculator and SMS buttons to fit them within the window boundaries.
Grid and Padding : Adjusted padding and grid settings to ensure elements fit nicely within the frame.
Font and Spacing : Consistent font usage and adequate spacing between elements for a neat layout.
Frame Background : Ensured the frame background color is uniform.
Now, the buttons should fit well within the boundary of the root window without any overflow. You can further adjust the sizes and spacing as needed to fine-tune the layout.
import tkinter as tk
from tkinter import scrolledtext
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_frames()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_frames(self):
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.root, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the entire root window
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
calc_button = tk.Button(
frame,
text="Calculator",
font=('Arial', 18),
width=15,
height=3,
bg="#4CAF50",
fg="white",
command=lambda: self.show_frame('calculator'))
calc_button.grid(row=1, column=0, padx=10, pady=10, sticky='nsew')
sms_button = tk.Button(
frame,
text="SMS",
font=('Arial', 18),
width=15,
height=3,
bg="#2196F3",
fg="white",
command=lambda: self.show_frame('sms'))
sms_button.grid(row=1, column=1, padx=10, pady=10, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_rowconfigure(4, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
frame.grid_columnconfigure(3, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=20)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=5, sticky='nsew')
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val, padx=5, pady=5, sticky='nsew')
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(
frame, wrap=tk.WORD, font=('Arial', 14), height=20, state=tk.DISABLED
)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
bg="#009688", fg="white",
command=self.send_message
)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen')
)
back_button.grid(row=3, column=0, columnspan=2, pady=20, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
# Normally, you'd handle the message sending logic here (e.g., API call)
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Adjustments Made:
Grid Configuration for Home Screen : Set grid row and column weights to make the home screen fill the root window and buttons to adjust properly.
Centralize Home Screen Elements : Used grid manager for better layout management.
Consistent Weight Configuration : This ensures that elements resize appropriately as the window size changes.
These adjustments should ensure that all the screens, especially the home screen, fill the root window, with buttons and other elements properly aligned and fitting within the boundaries.
import tkinter as tk
from tkinter import scrolledtext
from PIL import Image, ImageTk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_frames()
self.create_icons()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_frames(self):
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.root, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the entire root window
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
def create_icons(self):
self.icons = {}
calc_image = Image.open("calculator.png") # Replace with your image path
calc_image = calc_image.resize((50, 50), Image.ANTIALIAS)
self.icons["calculator"] = ImageTk.PhotoImage(calc_image)
sms_image = Image.open("sms.png") # Replace with your image path
sms_image = sms_image.resize((50, 50), Image.ANTIALIAS)
self.icons["sms"] = ImageTk.PhotoImage(sms_image)
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
calc_button = tk.Button(
frame,
text="Calculator",
font=('Arial', 18),
width=15,
height=3,
compound=tk.TOP,
image=self.icons["calculator"],
bg="#4CAF50",
fg="white",
command=lambda: self.show_frame('calculator')
)
calc_button.grid(row=1, column=0, padx=10, pady=10, sticky='nsew')
sms_button = tk.Button(
frame,
text="SMS",
font=('Arial', 18),
width=15,
height=3,
compound=tk.TOP,
image=self.icons["sms"],
bg="#2196F3",
fg="white",
command=lambda: self.show_frame('sms')
)
sms_button.grid(row=1, column=1, padx=10, pady=10, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_rowconfigure(4, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
frame.grid_columnconfigure(3, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=20)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=5, sticky='nsew')
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val, padx=5, pady=5, sticky='nsew')
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(
frame, wrap=tk.WORD, font=('Arial', 14), height=20, state=tk.DISABLED
)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
bg="#009688", fg="white",
command=self.send_message
)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen')
)
back_button.grid(row=3, column=0, columnspan=2, pady=20, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
# Normally, you'd handle the message sending logic here (e.g., API call)
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Explanation:
Create Icons Method : This method loads images using the PIL.Image module and resizes them appropriately. Make sure to replace "calculator.png" and "sms.png" with paths to your image files.
Adding Icons to Buttons : Added the image parameter to the buttons along with the compound=tk.TOP parameter to place text below the image.
Install PIL Library : You might need to install the Pillow library if you haven't already. Use the command pip install pillow to do so.
This should give your home screen buttons a more modern look with icons. Adjust the sizes as needed to ensure everything fits well within the window boundaries.
import tkinter as tk
from tkinter import scrolledtext
from PIL import Image, ImageTk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_frames()
self.create_icons()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_frames(self):
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.root, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the entire root window
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
def create_icons(self):
self.icons = {}
calc_image = Image.open("calculator.png") # Replace with your image path
calc_image = calc_image.resize((50, 50), Image.Resampling.LANCZOS)
self.icons["calculator"] = ImageTk.PhotoImage(calc_image)
sms_image = Image.open("sms.png") # Replace with your image path
sms_image = sms_image.resize((50, 50), Image.Resampling.LANCZOS)
self.icons["sms"] = ImageTk.PhotoImage(sms_image)
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
calc_button = tk.Button(
frame,
text="Calculator",
font=('Arial', 18),
width=15,
height=3,
compound=tk.TOP,
image=self.icons["calculator"],
bg="#4CAF50",
fg="white",
command=lambda: self.show_frame('calculator')
)
calc_button.grid(row=1, column=0, padx=10, pady=10, sticky='nsew')
sms_button = tk.Button(
frame,
text="SMS",
font=('Arial', 18),
width=15,
height=3,
compound=tk.TOP,
image=self.icons["sms"],
bg="#2196F3",
fg="white",
command=lambda: self.show_frame('sms')
)
sms_button.grid(row=1, column=1, padx=10, pady=10, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_rowconfigure(4, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
frame.grid_columnconfigure(3, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=20)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=5, sticky='nsew')
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val, padx=5, pady=5, sticky='nsew')
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(
frame, wrap=tk.WORD, font=('Arial', 14), height=20, state=tk.DISABLED
)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
bg="#009688", fg="white",
command=self.send_message
)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen')
)
back_button.grid(row=3, column=0, columnspan=2, pady=20, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
# Normally, you'd handle the message sending logic here (e.g., API call)
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Summary of Changes:
Used Image.Resampling.LANCZOS instead of the deprecated Image.ANTIALIAS when resizing images.
Additional consistency checks and image resizing.
Ensure you have Pillow installed by running:
sh Copy code
pip install pillow
Replace "calculator.png" and "sms.png" with the actual paths to your images. This should now correctly display the images without causing any attribute errors.
import tkinter as tk
from tkinter import scrolledtext
from PIL import Image, ImageTk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_header()
self.create_footer()
self.create_frames()
self.create_icons()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_header(self):
header_frame = tk.Frame(self.root, bg="#333333", height=50)
header_frame.grid(row=0, column=0, sticky="nsew")
header_label = tk.Label(header_frame, text="Smartphone", font=('Arial', 18), bg="#333333", fg="white")
header_label.pack(padx=20, pady=10)
def create_footer(self):
footer_frame = tk.Frame(self.root, bg="#333333", height=50)
footer_frame.grid(row=2, column=0, sticky="nsew")
footer_label = tk.Label(footer_frame, text="Footer", font=('Arial', 14), bg="#333333", fg="white")
footer_label.pack(padx=20, pady=10)
def create_frames(self):
main_frame = tk.Frame(self.root)
main_frame.grid(row=1, column=0, sticky="nsew")
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(main_frame, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the entire root window
self.root.grid_rowconfigure(0, weight=0) # Header
self.root.grid_rowconfigure(1, weight=1) # Main content area
self.root.grid_rowconfigure(2, weight=0) # Footer
self.root.grid_columnconfigure(0, weight=1)
def create_icons(self):
self.icons = {}
calc_image = Image.open("calculator.png") # Replace with your image path
calc_image = calc_image.resize((50, 50), Image.Resampling.LANCZOS)
self.icons["calculator"] = ImageTk.PhotoImage(calc_image)
sms_image = Image.open("sms.png") # Replace with your image path
sms_image = sms_image.resize((50, 50), Image.Resampling.LANCZOS)
self.icons["sms"] = ImageTk.PhotoImage(sms_image)
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
calc_button = tk.Button(
frame,
text="Calculator",
font=('Arial', 18),
width=15,
height=3,
compound=tk.TOP,
image=self.icons["calculator"],
bg="#4CAF50",
fg="white",
command=lambda: self.show_frame('calculator')
)
calc_button.grid(row=1, column=0, padx=10, pady=10, sticky='nsew')
sms_button = tk.Button(
frame,
text="SMS",
font=('Arial', 18),
width=15,
height=3,
compound=tk.TOP,
image=self.icons["sms"],
bg="#2196F3",
fg="white",
command=lambda: self.show_frame('sms')
)
sms_button.grid(row=1, column=1, padx=10, pady=10, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_rowconfigure(4, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
frame.grid_columnconfigure(3, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=20)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=5, sticky='nsew')
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val, padx=5, pady=5, sticky='nsew')
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(
frame, wrap=tk.WORD, font=('Arial', 14), height=20, state=tk.DISABLED
)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
bg="#009688", fg="white",
command=self.send_message
)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen')
)
back_button.grid(row=3, column=0, columnspan=2, pady=20, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
# Normally, you'd handle the message sending logic here (e.g., API call)
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Explanation:
Header and Footer Creation : Added create_header and create_footer methods to create the header and footer frames with appropriate dimensions and styling.
Main Frame Adjustments : The main content area (main frame) is placed between the header and footer and is made to fill the remaining space.
Grid Configuration : Adjusted grid rows to ensure the header, main content area, and footer display correctly without overlapping.
This structure ensures that the header and footer remain static while the main content area changes based on the selected screen. The layout dynamically adjusts to ensure everything fits within the application window properly.
import tkinter as tk
from tkinter import scrolledtext
from PIL import Image, ImageTk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_header()
self.create_footer()
self.create_frames()
self.create_icons()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_header(self):
self.header_frame = tk.Frame(self.root, bg="#333333", height=50)
self.header_frame.grid(row=0, column=0, sticky="ew")
self.header_frame.grid_propagate(False)
header_label = tk.Label(self.header_frame, text="Smartphone Interface", font=('Arial', 18), bg="#333333", fg="white")
header_label.pack(side=tk.LEFT, padx=10)
def create_footer(self):
self.footer_frame = tk.Frame(self.root, bg="#333333", height=50)
self.footer_frame.grid(row=2, column=0, sticky="ew")
self.footer_frame.grid_propagate(False)
footer_label = tk.Label(self.footer_frame, text="Footer", font=('Arial', 14), bg="#333333", fg="white")
footer_label.pack(side=tk.RIGHT, padx=10)
def create_frames(self):
self.main_frame = tk.Frame(self.root, bg="#f0f0f0")
self.main_frame.grid(row=1, column=0, sticky="nsew")
self.main_frame.grid_propagate(True) # Allow the frame to adjust to content
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.main_frame, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the entire root window
self.root.grid_rowconfigure(0, weight=0) # Header
self.root.grid_rowconfigure(1, weight=1) # Main content area
self.root.grid_rowconfigure(2, weight=0) # Footer
self.root.grid_columnconfigure(0, weight=1)
def create_icons(self):
self.icons = {}
calc_image = Image.open("calculator.png") # Replace with your image path
calc_image = calc_image.resize((50, 50), Image.Resampling.LANCZOS)
self.icons["calculator"] = ImageTk.PhotoImage(calc_image)
sms_image = Image.open("sms.png") # Replace with your image path
sms_image = sms_image.resize((50, 50), Image.Resampling.LANCZOS)
self.icons["sms"] = ImageTk.PhotoImage(sms_image)
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
calc_button = tk.Button(
frame,
text="Calculator",
font=('Arial', 18),
compound=tk.TOP,
image=self.icons["calculator"],
bg="#4CAF50",
fg="white",
command=lambda: self.show_frame('calculator')
)
calc_button.grid(row=1, column=0, padx=20, pady=20, sticky='nsew')
sms_button = tk.Button(
frame,
text="SMS",
font=('Arial', 18),
compound=tk.TOP,
image=self.icons["sms"],
bg="#2196F3",
fg="white",
command=lambda: self.show_frame('sms')
)
sms_button.grid(row=1, column=1, padx=20, pady=20, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_rowconfigure(4, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_columnconfigure(2, weight=1)
frame.grid_columnconfigure(3, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=20)
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=5, sticky='nsew')
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
row_val = 2
col_val = 0
for button in buttons:
command = lambda x=button: self.press_calc_button(x)
tk.Button(
frame,
text=button,
padx=20,
pady=20,
font=('Arial', 18),
command=command
).grid(row=row_val, column=col_val, padx=5, pady=5, sticky='nsew')
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=row_val, column=0, columnspan=4, pady=20, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_rowconfigure(3, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(
frame, wrap=tk.WORD, font=('Arial', 14), height=20, state=tk.DISABLED
)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(
frame,
text="Send",
font=('Arial', 14),
bg="#009688", fg="white",
command=self.send_message
)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(
frame,
text="Back to Home",
font=('Arial', 18),
bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen')
)
back_button.grid(row=3, column=0, columnspan=2, pady=20, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Improvements Made:
Header and Footer Design :
Added padding and aligned text to the sides.
Used consistent grid configuration to ensure proper placement.
Home Screen Layout :
Proper spacing between the elements to improve overall aesthetics.
Unified button styling and padding to improve the visual consistency.
Main Content Area :
Consistent padding and proper alignment for elements.
Made sure the grid configuration dynamically adjusts based on the size of the window.
Calculator and SMS Layout :
Consistent padding and configuration for the calculator and SMS app screens with appropriate weights.
These enhancements aim to create a more polished and user-friendly interface, ensuring all elements are well-aligned and responsive to changes in the window size.
import tkinter as tk
from tkinter import scrolledtext
from PIL import Image, ImageTk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_header()
self.create_footer()
self.create_main_frame()
self.create_icons()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_header(self):
self.header_frame = tk.Frame(self.root, bg="#333333", height=50)
self.header_frame.grid(row=0, column=0, sticky="ew")
self.header_frame.grid_propagate(False)
header_label = tk.Label(self.header_frame, text="Smartphone Interface", font=('Arial', 18), bg="#333333", fg="white")
header_label.pack(padx=10, pady=10)
def create_footer(self):
self.footer_frame = tk.Frame(self.root, bg="#333333", height=50)
self.footer_frame.grid(row=2, column=0, sticky="ew")
self.footer_frame.grid_propagate(False)
footer_label = tk.Label(self.footer_frame, text="Footer", font=('Arial', 14), bg="#333333", fg="white")
footer_label.pack(padx=10, pady=10)
def create_main_frame(self):
self.main_frame = tk.Frame(self.root, bg="#f0f0f0")
self.main_frame.grid(row=1, column=0, sticky="nsew")
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.main_frame, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the root window
self.root.grid_rowconfigure(0, weight=0) # Header
self.root.grid_rowconfigure(1, weight=1) # Main content area
self.root.grid_rowconfigure(2, weight=0) # Footer
self.root.grid_columnconfigure(0, weight=1)
def create_icons(self):
self.icons = {}
calc_image = Image.open("calculator.png").resize((50, 50), Image.Resampling.LANCZOS)
self.icons["calculator"] = ImageTk.PhotoImage(calc_image)
sms_image = Image.open("sms.png").resize((50, 50), Image.Resampling.LANCZOS)
self.icons["sms"] = ImageTk.PhotoImage(sms_image)
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20)
calc_button = tk.Button(frame, text="Calculator", font=('Arial', 18), image=self.icons["calculator"], compound=tk.TOP,
bg="#4CAF50", fg="white", command=lambda: self.show_frame('calculator'))
calc_button.grid(row=1, column=0, padx=20, pady=20, sticky='nsew')
sms_button = tk.Button(frame, text="SMS", font=('Arial', 18), image=self.icons["sms"], compound=tk.TOP,
bg="#2196F3", fg="white", command=lambda: self.show_frame('sms'))
sms_button.grid(row=1, column=1, padx=20, pady=20, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
for i in range(5):
frame.grid_rowconfigure(i, weight=1)
for i in range(4):
frame.grid_columnconfigure(i, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=10, sticky='n')
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=2, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=10, sticky='nsew')
buttons = ['7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', 'C', '0', '=', '+']
for idx, button in enumerate(buttons):
command = lambda x=button: self.press_calc_button(x)
tk.Button(frame, text=button, font=('Arial', 18), command=command).grid(row=2 + idx // 4, column=idx % 4,
padx=5, pady=5, sticky='nsew')
back_button = tk.Button(frame, text="Back to Home", font=('Arial', 18), bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=6, column=0, columnspan=4, pady=10, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(frame, wrap=tk.WORD, font=('Arial', 14), state=tk.DISABLED)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(frame, text="Send", font=('Arial', 14), bg="#009688", fg="white", command=self.send_message)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(frame, text="Back to Home", font=('Arial', 18), bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=3, column=0, columnspan=2, pady=10, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Explanation:
Header and Footer Design :
Used the grid_propagate(False) method to set a fixed height.
Aligned text to the middle.
Main Frame Design :
The main content area is placed between the header and footer and set to expand.
Improved button and widget placement for a consistent UI.
Calculator and SMS Apps Improvements :
Used a loop for dynamic button placement in the Calculator app.
Improved alignment and padding for the SMS app.
This version aims for a clean, polished look with proper alignment, spacing, and dynamic resizing capabilities. Please adjust the dimensions, spacing, and colors according to your project's specific requirements.
import tkinter as tk
from tkinter import scrolledtext
from PIL import Image, ImageTk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.create_header()
self.create_footer()
self.create_main_frame()
self.create_icons()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_header(self):
self.header_frame = tk.Frame(self.root, bg="#333333", height=50)
self.header_frame.grid(row=0, column=0, sticky="ew")
self.header_frame.grid_propagate(False)
header_label = tk.Label(self.header_frame, text="Smartphone Interface", font=('Arial', 18), bg="#333333", fg="white")
header_label.pack(padx=10, pady=10)
def create_footer(self):
self.footer_frame = tk.Frame(self.root, bg="#333333", height=50)
self.footer_frame.grid(row=2, column=0, sticky="ew")
self.footer_frame.grid_propagate(False)
footer_label = tk.Label(self.footer_frame, text="Footer", font=('Arial', 14), bg="#333333", fg="white")
footer_label.pack(padx=10, pady=10)
def create_main_frame(self):
self.main_frame = tk.Frame(self.root, bg="#f0f0f0")
self.main_frame.grid(row=1, column=0, sticky="nsew")
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.main_frame, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the root window
self.root.grid_rowconfigure(0, weight=0) # Header
self.root.grid_rowconfigure(1, weight=1) # Main content area
self.root.grid_rowconfigure(2, weight=0) # Footer
self.root.grid_columnconfigure(0, weight=1)
def create_icons(self):
self.icons = {}
calc_image = Image.open("calculator.png").resize((40, 40), Image.Resampling.LANCZOS)
self.icons["calculator"] = ImageTk.PhotoImage(calc_image)
sms_image = Image.open("sms.png").resize((40, 40), Image.Resampling.LANCZOS)
self.icons["sms"] = ImageTk.PhotoImage(sms_image)
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20)
calc_button = tk.Button(frame, text="Calculator", font=('Arial', 12), image=self.icons["calculator"], compound=tk.TOP,
bg="#4CAF50", fg="white", command=lambda: self.show_frame('calculator'))
calc_button.grid(row=1, column=0, padx=10, pady=10, sticky='nsew')
sms_button = tk.Button(frame, text="SMS", font=('Arial', 12), image=self.icons["sms"], compound=tk.TOP,
bg="#2196F3", fg="white", command=lambda: self.show_frame('sms'))
sms_button.grid(row=1, column=1, padx=10, pady=10, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
for i in range(5):
frame.grid_rowconfigure(i, weight=1)
for i in range(4):
frame.grid_columnconfigure(i, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=10, sticky='n')
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=2, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=10, sticky='nsew')
buttons = ['7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', 'C', '0', '=', '+']
for idx, button in enumerate(buttons):
command = lambda x=button: self.press_calc_button(x)
tk.Button(frame, text=button, font=('Arial', 18), command=command).grid(row=2 + idx // 4, column=idx % 4,
padx=5, pady=5, sticky='nsew')
back_button = tk.Button(frame, text="Back to Home", font=('Arial', 18), bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=6, column=0, columnspan=4, pady=10, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(frame, wrap=tk.WORD, font=('Arial', 14), state=tk.DISABLED)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(frame, text="Send", font=('Arial', 14), bg="#009688", fg="white", command=self.send_message)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(frame, text="Back to Home", font=('Arial', 18), bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=3, column=0, columnspan=2, pady=10, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Changes Made:
Icon Size Reduction : Reduced the size of the icons to (40x40) to ensure buttons fit nicely.
Button Font Size : Reduced the font size of the text on the home screen buttons to make them more compact.
Padding and Margin Adjustments : Adjusted padding and margins to ensure elements fit within the available space without overflow.
Grid Configuration : Ensured buttons are properly aligned and spaced within the grid.
This should result in a more compact and visually appealing home screen with buttons that fit well within the boundaries of the root window. Adjust the sizes and spacing further if needed for your specific design requirements.
import tkinter as tk
from tkinter import scrolledtext
from PIL import Image, ImageTk
class SmartphoneInterface:
def __init__(self, root):
self.root = root
self.root.title("Smartphone Interface")
self.root.geometry('400x700')
self.messages = [] # Store SMS messages
self.unread_sms_count = 0 # Track unread SMS count
self.create_header()
self.create_footer()
self.create_main_frame()
self.create_icons()
self.create_home_screen()
self.create_calculator_app()
self.create_sms_app()
self.show_frame('home_screen')
def create_header(self):
self.header_frame = tk.Frame(self.root, bg="#333333", height=50)
self.header_frame.grid(row=0, column=0, sticky="ew")
self.header_frame.grid_propagate(False)
self.header_label = tk.Label(self.header_frame, text="Smartphone Interface", font=('Arial', 18), bg="#333333", fg="white")
self.header_label.pack(side=tk.LEFT, padx=10)
self.notification_label = tk.Label(self.header_frame, text="", font=('Arial', 12), bg="#333333", fg="red")
self.notification_label.pack(side=tk.RIGHT, padx=10)
def create_footer(self):
self.footer_frame = tk.Frame(self.root, bg="#333333", height=50)
self.footer_frame.grid(row=2, column=0, sticky="ew")
self.footer_frame.grid_propagate(False)
footer_label = tk.Label(self.footer_frame, text="Footer", font=('Arial', 14), bg="#333333", fg="white")
footer_label.pack(padx=10, pady=10)
def create_main_frame(self):
self.main_frame = tk.Frame(self.root, bg="#f0f0f0")
self.main_frame.grid(row=1, column=0, sticky="nsew")
self.frames = {}
for frame_name in ['home_screen', 'calculator', 'sms']:
frame = tk.Frame(self.main_frame, bg="#f0f0f0")
frame.grid(row=0, column=0, sticky='nsew')
self.frames[frame_name] = frame
# Configure grid weights for the entire root window
self.root.grid_rowconfigure(0, weight=0) # Header
self.root.grid_rowconfigure(1, weight=1) # Main content area
self.root.grid_rowconfigure(2, weight=0) # Footer
self.root.grid_columnconfigure(0, weight=1)
def create_icons(self):
self.icons = {}
calc_image = Image.open("calculator.png").resize((40, 40), Image.Resampling.LANCZOS)
self.icons["calculator"] = ImageTk.PhotoImage(calc_image)
sms_image = Image.open("sms.png").resize((40, 40), Image.Resampling.LANCZOS)
self.icons["sms"] = ImageTk.PhotoImage(sms_image)
def show_frame(self, frame_name):
if frame_name == 'sms':
self.unread_sms_count = 0 # Reset unread SMS count when viewing SMS screen
self.update_notification_label()
frame = self.frames[frame_name]
frame.tkraise()
def update_notification_label(self):
if self.unread_sms_count > 0:
self.notification_label.config(text=f"{self.unread_sms_count} New SMS")
else:
self.notification_label.config(text="")
def create_home_screen(self):
frame = self.frames['home_screen']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="Home Screen", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20)
calc_button = tk.Button(frame, text="Calculator", font=('Arial', 12), image=self.icons["calculator"], compound=tk.TOP,
bg="#4CAF50", fg="white", command=lambda: self.show_frame('calculator'))
calc_button.grid(row=1, column=0, padx=10, pady=10, sticky='nsew')
sms_button = tk.Button(frame, text="SMS", font=('Arial', 12), image=self.icons["sms"], compound=tk.TOP,
bg="#2196F3", fg="white", command=lambda: self.show_frame('sms'))
sms_button.grid(row=1, column=1, padx=10, pady=10, sticky='nsew')
def create_calculator_app(self):
frame = self.frames['calculator']
for i in range(5):
frame.grid_rowconfigure(i, weight=1)
for i in range(4):
frame.grid_columnconfigure(i, weight=1)
title = tk.Label(frame, text="Calculator", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=4, pady=10, sticky='n')
self.calc_screen = tk.Entry(frame, font=('Arial', 24), bd=10, insertwidth=2, borderwidth=4)
self.calc_screen.grid(row=1, column=0, columnspan=4, pady=10, padx=10, sticky='nsew')
buttons = ['7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', 'C', '0', '=', '+']
for idx, button in enumerate(buttons):
command = lambda x=button: self.press_calc_button(x)
tk.Button(frame, text=button, font=('Arial', 18), command=command).grid(row=2 + idx // 4, column=idx % 4,
padx=5, pady=5, sticky='nsew')
back_button = tk.Button(frame, text="Back to Home", font=('Arial', 18), bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=6, column=0, columnspan=4, pady=10, sticky='nsew')
def press_calc_button(self, key):
if key == '=':
try:
result = str(eval(self.calc_screen.get()))
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, result)
except:
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, 'Error')
elif key == 'C':
self.calc_screen.delete(0, tk.END)
else:
current = self.calc_screen.get()
if current == "Error":
self.calc_screen.delete(0, tk.END)
current = ""
self.calc_screen.delete(0, tk.END)
self.calc_screen.insert(0, current + key)
def create_sms_app(self):
frame = self.frames['sms']
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=8)
frame.grid_rowconfigure(2, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
title = tk.Label(frame, text="SMS", font=('Arial', 24), bg="#f0f0f0")
title.grid(row=0, column=0, columnspan=2, pady=20, sticky='n')
self.sms_display = scrolledtext.ScrolledText(frame, wrap=tk.WORD, font=('Arial', 14), state=tk.DISABLED)
self.sms_display.grid(row=1, column=0, columnspan=2, pady=10, padx=10, sticky='nsew')
self.message_entry = tk.Entry(frame, font=('Arial', 14))
self.message_entry.grid(row=2, column=0, padx=5, pady=10, sticky='ew')
send_button = tk.Button(frame, text="Send", font=('Arial', 14), bg="#009688", fg="white", command=self.send_message)
send_button.grid(row=2, column=1, padx=5, pady=10, sticky='ew')
back_button = tk.Button(frame, text="Back to Home", font=('Arial', 18), bg="#f44336", fg="white",
command=lambda: self.show_frame('home_screen'))
back_button.grid(row=3, column=0, columnspan=2, pady=10, sticky='nsew')
def send_message(self):
message = self.message_entry.get()
if message:
self.messages.append(f"You: {message}")
self.message_entry.delete(0, tk.END)
self.update_sms_display()
def update_sms_display(self):
self.sms_display.config(state=tk.NORMAL)
self.sms_display.delete(1.0, tk.END)
for message in self.messages:
self.sms_display.insert(tk.END, f"{message}\n")
self.sms_display.config(state=tk.DISABLED)
self.unread_sms_count += 1 # Increment unread SMS count
self.update_notification_label()
if __name__ == "__main__":
root = tk.Tk()
app = SmartphoneInterface(root)
root.mainloop()
Explanation:
Notification Label in Header : Added a notification_label to the header frame.
Unread SMS Count : Introduced unread_sms_count to track the number of new messages.
Update Notification Label : The update_notification_label method updates the text of notification_label based on unread_sms_count.
Reset Unread Count : Reset the unread_sms_count when the user navigates to the SMS screen.
Incremente Unread Count on New Message : Increased the unread_sms_count when a new message is added and updated the notification label accordingly.
This implementation ensures that users are notified of new SMS messages via a notification badge in the header, which updates dynamically as new messages are received and viewed.