Python-courses-python-exercises-python-exception-python-pip

Exercise 49

1- Create a class called TK_extended which inherits from TK class and having the attributes:
- Master: that represents the name of the main window
- title: that represents the title of the main window
2 - Create a method called create() that creates the window
3 - Create a method called resize(width, height) that can resize the window.
4 - Create a method called generate() to generate the window

Solution

from tkinter import*
# Question 1
class TK_extended(Tk):

def __init__(self, master , title):
self.master = master
self.title = title
#------------
# Question 2
#------------
def create(self):
self.master = Tk()
self.master.title(self.title)

#------------
# Question 3
#------------
def resize(self, width , height):
self.master.geometry("{}x{}".format(width , height))

#------------
# Question 4
#------------
def generate(self):
self.master.mainloop()


#------------
# Execution
#------------
root = TK_extended("win" , "My Window")
root.create()
root.resize(500 , 300)
root.generate()

Younes Derfoufi
my-courses.net

Leave a Reply