Updating the FriendsList class

We still have a Friends menu at the top of our FriendsList class, but this doesn't make much sense as a place to put our avatar uploading functionality. Instead, we can create a new submenu called avatar:

...
from avatarwindow import AvatarWindow
...
def __init__(self, **kwargs):
...
self.avatar_menu = tk.Menu(self.menu, fg="black",
bg="lightgrey", tearoff=0)
self.avatar_menu.add_command(label="Change Avatar",
command=self.change_avatar)

self.menu.add_cascade(label="Friends", menu=self.friends_menu)
self.menu.add_cascade(label="Avatar", menu=self.avatar_menu)

After importing our new class, we create a menu called avatar_menu to hold a Change Avatar function. This will call a method named change_avatar:

def change_avatar(self):
AvatarWindow(self)

All that our change_avatar method needs to do is spawn an instance of our AvatarWindow, since everything else is contained in that class.

With this finished, we can now spawn the AvatarWindow and upload an image to serve as our avatar. Go ahead and run your friendslist.py file, log in, and then pick Avatar | Change Avatar from the top-menu:

Allowing the user to choose any image could lead to problems with our layout if a user chooses a very large one, since Tkinter itself cannot resize images. Try opening a very big image and see how it does not properly fit into the AvatarWindow. To handle this, we will need to use a library called Python Imaging Library (PIL) to scale down the chosen image.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset